locked
Javascript function not working within a href tag windows phone 8.1 and windows 8.1

    Question

  • I am working on WP8.1. Where I am using webview. The data comes from the server and I am loading the content using webview.NavigateToString(). The script contains following

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>JS Bin</title>
    </head>
    <body>

    </body>

      <script type="text/javascript">
        function Action()
    {
    window.external.notify('hi');
    }

    document.write('<a href="javascript:Action();">test</a>');
      </script>
    </html>

    This code contains calling javascript function from "a href" tag. So whenever the user clicks that javascript function will be called. But in Wp8.1 whenever i click on the image, the javascript function is not called. I tried registering with onclick()

      <a onclick="Action();">

    This is working. But as per project is concerned (consistency across all platforms) the server will provide
      <a href="javascript:Action();"> javascript function.

    The above function working fine on IE10,IE11 on the desktop but is not working on IE11 on windows phone 8.1.

    Loading the content into Webview using webview.NavigateToString() method and clicking the tag doesn't fire ScriptNotify event.

    Please help.

    • Edited by nucleons Sunday, November 30, 2014 3:15 PM
    Sunday, November 30, 2014 9:37 AM

Answers

  • I solved it using getting the html content from server, saving the file as myhtml.html using Windows.ApplicationModel.Package.Current.InstalledLocation and navigating using this

    webbrowser.Navigate(new Uri(ms-appx-web:///myhtml.html));

    Thanks

    • Marked as answer by nucleons Tuesday, December 09, 2014 2:56 PM
    Tuesday, December 09, 2014 2:44 PM

All replies

  • I am working on WP8.1. Where I am using webview. The data comes from the server and I am loading the content using webview.NavigateToString(). The script contains following

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>JS Bin</title>
    </head>
    <body>

    </body>

      <script type="text/javascript">
        function Action()
    {
    window.external.notify('hi');
    }

    document.write('<a href="javascript:Action();">test</a>');
      </script>
    </html>

    This code contains calling javascript function from "a href" tag. So whenever the user clicks that javascript function will be called. But in Wp8.1 whenever i click on the image, the javascript function is not called. I tried registering with onclick()

      <a onclick="Action();">

    This is working. But as per project is concerned (consistency across all platforms)the server will provide

      <a href="javascript:Action();"> javascript function.

    The above function working fine on IE10,IE11 on the desktop but is not working on IE11 on windows phone 8.1.

    Loading the content into Webview and clicking the tag doesn't fire ScriptNotify event.

    Please help.
    Friday, November 28, 2014 11:46 AM
  • Hi Adityak1,
    Did you add the domain into the AllowedScriptNotifyUris property of WebView control? According to your code snippet, you want to pass a string parameter to your app using ScriptNotify event. To enable an external web page to fire the ScriptNotify event when calling window.external.notify, you must include the page's URI in the ApplicationContentUriRules section of the app manifest. You can find more information from http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.webview.scriptnotify.aspx.
    If this does not help, please post more information about your scenario.
    Regards,
    Monday, December 01, 2014 6:31 AM
    Moderator
  • I solved it using getting the html content from server, saving the file as myhtml.html using Windows.ApplicationModel.Package.Current.InstalledLocation and navigating using this

    webbrowser.Navigate(new Uri(ms-appx-web:///myhtml.html));

    Thanks

    • Marked as answer by nucleons Tuesday, December 09, 2014 2:56 PM
    Tuesday, December 09, 2014 2:44 PM