I'm writing a Windows 8 application in C# and XAML that hosts a Web page in a WebView. The Web page makes calls to alert() and confirm(), but the WebView throws exceptions, saying, "'alert' is undefined," or "'confirm' is undefined." Can alert() and confirm() be enabled, or do I need to write code to emulate their normal function? And if I need to write them myself, how should I begin such an undertaking? Thank you.
3 Answers
Alert and confirm will not work from a WebView. You can use WebView.ScriptNotify to receive an event from your script on your page and use that event to show a dialog box using the MessageDialog class.
In your script where you want the alert ...
window.external.notify('foo');
And use the example in the WebView.ScriptNotify method in your C#.
6 Comments
var answer=window.external.notify("confirm('Yes or no?')") But how would I be able to stop the JavaScript, let the user answer, and then return the value to "answer"?You need to do some workaround. I did this and it worked for me.
window.alert = function(__msg){window.external.notify(' + __msg + ');};
refer a similar question of mine for windows phone here
Comments
You will need to write code to enable them. Note that this is true in a HTML5 Win8 app as well as XAML.
For script that can access WinRT, you can use Windows.UI.Popup.MessageDialog:
(new Windows.UI.Popup.MessageDialog("Hello!", "Welcome")).showAsync().done(dismissedHandler)
Note that these are async, and not block execution.
For acquiring information form the user -- e.g text input from the user, there is nothing standard, and you'll need to create a new set of mark up for that.