4

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 3

5

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#.

Sign up to request clarification or add additional context in comments.

6 Comments

I have actually looked into those options already, but if I use window.external.notify for a confirm, how would I obtain its return value without major changes to my code? For example, I could say, 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"?
Perhaps WebView.InvokeScript will work. msdn.microsoft.com/en-us/library/windows/apps/…
@user1325179 - Hi, I am facing a similar issue in WP 8.1.. did you find a solution to this ? I am able to show the message dialog with the confirm message , but how to return yes/no ?
@nipun.birla, I had to write HTML and JavaScript replacements. For example, instead of a confirm() call, I had to make a form with yes and no buttons and then attach JavaScript functions to each button. It's all asynchronous. Making the switch was hard at first because I had to rewrite a lot of code that depended on a synchronous call to these sorts of functions, especially alert().
@user1325179 - Thanks for the reply, I found another solution in the meantime : stackoverflow.com/questions/31804380/… O:)
|
3

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

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.