locked
Javascript's alert, confirm and prompt doesn't work in Visual Studio Express 11 (for developer preview) RRS feed

  • Question

  • <!DOCTYPE html>
    <html>
    <head>
    
        <meta charset="utf-8" />
        <title>SimpleProject1</title>
    
        <!-- WinJS references -->
        <link rel="stylesheet" href="/winjs/css/ui-dark.css" />
        <script src="/winjs/js/base.js"></script>
        <script src="/winjs/js/wwaapp.js"></script>
        
        <!-- SimpleProject1 references -->
        <link rel="stylesheet" href="/css/default.css" />
        <script src="/js/default.js"></script>
    
        <script type="text/javascript">
            function show_alert() {
                alert("Test Alert!");
            }
        </script>
    
      </head>
    <body>
    
    
        <h1>My First Web Page</h1>
    
    
        <input type="button" onclick="show_alert()" value="Show prompt box" />
     
    </body>
    </html>
    
    

    I have used the above simple JavaScript code (in default.html) to test alert and it doesn't work when I am trying to run it (without debugging) from Visual Studio 11. If I copy the same code into any html file and run it, everything works. If I try to debug in Visual Studio 11, by putting a break point at 'alert', it just errors out saying that 'alert is undefined'. I just started using JavaScript in VS 11 on Windows 8 Developer Preview. I guess this is simple but couldn't figure it out. Any help would be greatly appreciated. Thanks!

    • Moved by Keith-Microsoft Support Tuesday, September 27, 2011 1:39 PM Move (From:Windows Developer Preview: General OS questions )
    Tuesday, September 27, 2011 6:52 AM

Answers

  • I think the MessageDialog WinRT class may be what you are looking for:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog

     

    Some quick example code:

    var md = new Windows.UI.Popups.MessageDialog("Hello World!");
    md.commands.append(new Windows.UI.Popups.UICommand("Yes"));
    md.commands.append(new Windows.UI.Popups.UICommand("No"));
    md.showAsync().then(function (command) { console.log("pressed: " + command.label); });
    


    You can omit the commands if you're not interested in user input, though you will still have to call "then" on showAsync() due to promises returned from WinRT not executing until they are chained off of (even if you have no work to chain off them.)

    Alert and friends actually blocked the UI thread (and therefore the JavaScript engine) so your app couldn't be responsive while the user contemplated clicking on them. These new popups are asynchronous so you can continue doing work while waiting for user interaction.

    Using MessageDialog is fine for debugging, but be careful to follow the user experience guidelines when putting them in your actual app -- as Chris said it's better to display messages in-line whenever possible and only use MessageDialog when the app is in a state it cannot continue from without user interaction.

    Cheers,

    -Jeff

     

    Tuesday, September 27, 2011 3:44 PM

All replies

  • A better place to ask this: http://social.msdn.microsoft.com/Forums/en-US/winappswithhtml5/threads

    Regards,

    Dylan Meeus


    0x2B |~ 0x2B Blog : www.it-ca.net/blogdylan
    Tuesday, September 27, 2011 12:44 PM
  • The WinJS API doesn't include the "alert" method. You'll need to try another method of displaying the value, like showing it within a <div> or <span> tag.

    The issue isn't Visual Studio 11, but rather the Metro Application environment. Visual Studio is just a tool for writing and debugging code, it's not the runtime enironment. Also, Metro apps don't run within IE10 "standard", but rather a modified runtime within the Metro UI.


    Microsoft MVP - Bing Maps
    Blog: http://pietschsoft.com | Web.Maps.VE - ASP.NET AJAX Bing Maps Server Control
    Tuesday, September 27, 2011 3:15 PM
  • I think the MessageDialog WinRT class may be what you are looking for:

    http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.popups.messagedialog

     

    Some quick example code:

    var md = new Windows.UI.Popups.MessageDialog("Hello World!");
    md.commands.append(new Windows.UI.Popups.UICommand("Yes"));
    md.commands.append(new Windows.UI.Popups.UICommand("No"));
    md.showAsync().then(function (command) { console.log("pressed: " + command.label); });
    


    You can omit the commands if you're not interested in user input, though you will still have to call "then" on showAsync() due to promises returned from WinRT not executing until they are chained off of (even if you have no work to chain off them.)

    Alert and friends actually blocked the UI thread (and therefore the JavaScript engine) so your app couldn't be responsive while the user contemplated clicking on them. These new popups are asynchronous so you can continue doing work while waiting for user interaction.

    Using MessageDialog is fine for debugging, but be careful to follow the user experience guidelines when putting them in your actual app -- as Chris said it's better to display messages in-line whenever possible and only use MessageDialog when the app is in a state it cannot continue from without user interaction.

    Cheers,

    -Jeff

     

    Tuesday, September 27, 2011 3:44 PM