none
Communication between HTML in WebBrowser and windows phone app RRS feed

  • Question

  • Hi All

    Need some urgent help, I have some static html files downloaded into Isolated store. I am trying to open those files using Web Browser control inside windows phone app. Now every HTML file will communicate with back-end code through javascript, and `window.external.notify()` is not able to apply for all html pages.  As this HTML files have common framework applied so not suitable to change this framework.

    So my question is,

    Is there any API/bridge like (in iOS) WebViewJavascriptBridge and GAJavaScript or like (android) Javascript Interface available for windows phone 8 which will communicate with HTML in WebBrowser and windows phone app. Will WinJS achieve the same for Windows Phone 8?

    Thanks in advance.

    Wednesday, August 13, 2014 4:52 AM

Answers

  • Hi onkar.nalawade,

    May I know whether the ‘communication’ (that between HTML in WebBrowser and WP app) you mean is you wanna call the JS functions and get the JS returned value in code behind? If so, in windows phone 8, I’d like to suggest you to use WebBrowser.InvokeScript Method: http://msdn.microsoft.com/en-us/library/windows/apps/ff402838(v=vs.105).aspx, please note we need to set the IsScriptEnabled property to true if you want to enable scripting in your control.

    Code snippet for your reference:

    • HTML Page & JS function:

    <html>

    <head>

        <title>This is a test page</title>

        <script>

    function HelloAlert(UserName)

    {

           alert("Hello"+UserName);

        return "Hello, "+UserName;

    }

        </script>

    </head>

    <body>

        Hello

    </body>

    </html>

    • Set WebBrowser control’s source:

    <phone:WebBrowser x:Name="wb" IsScriptEnabled="True" Source="/Test.html" />

    • Call the JS and get the return value in code behind:

    publicMainPage()

            {

                InitializeComponent();

                this.wb.LoadCompleted += wb_LoadCompleted;

            }

            voidwb_LoadCompleted(objectsender, NavigationEventArgse)

            {

                varreturnValue = wb.InvokeScript("HelloAlert","Alan Yao");

            }

    • Screenshot:

    In windows phone 8.1, if you're using JavaScript to build the app, then it is not needed to save static html pages to local storage and display them into some controls, instead you could simply display the pages inside your app directly and add whatever script you want. You may want to refer to http://blogs.msdn.com/b/thunbrynt/archive/2014/04/14/windows-phone-8-1-for-developers-for-the-html-javascript-developer.aspx for more information.

    Best Regards,

    Ming Xu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 18, 2014 3:13 PM

All replies

  • Hi,

    Thank for asking question in windows phone forum.

    Could you please have a try on http://msdn.microsoft.com/en-us/library/windows/apps/microsoft.phone.controls.webbrowser.scriptnotify(v=vs.105).aspx

    To say if you can get the event fired?


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    Alan Yao
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Wednesday, August 13, 2014 8:28 AM
    Moderator
  • Thanks @Alan_Yao for reply, I have tried this and its working perfect but for that I have to mention window.external.notify() inside my javascript function. Will it possible that, I can able to catch default javascript return from my code behind. As my HTML files are have some default framework and changing this framework is not that feasible to me. Can you please suggest any other workaround to achieve same functionality.
    Wednesday, August 13, 2014 9:02 AM
  • @Alan_Yao, Is there any Javascript API or bridge API (like iOS and Android have). Other than window.external.notify() is there any other way to communicate with windows phone app to html inside browser? waiting for help thanks.
    Monday, August 18, 2014 5:09 AM
  • Hi onkar.nalawade,

    May I know whether the ‘communication’ (that between HTML in WebBrowser and WP app) you mean is you wanna call the JS functions and get the JS returned value in code behind? If so, in windows phone 8, I’d like to suggest you to use WebBrowser.InvokeScript Method: http://msdn.microsoft.com/en-us/library/windows/apps/ff402838(v=vs.105).aspx, please note we need to set the IsScriptEnabled property to true if you want to enable scripting in your control.

    Code snippet for your reference:

    • HTML Page & JS function:

    <html>

    <head>

        <title>This is a test page</title>

        <script>

    function HelloAlert(UserName)

    {

           alert("Hello"+UserName);

        return "Hello, "+UserName;

    }

        </script>

    </head>

    <body>

        Hello

    </body>

    </html>

    • Set WebBrowser control’s source:

    <phone:WebBrowser x:Name="wb" IsScriptEnabled="True" Source="/Test.html" />

    • Call the JS and get the return value in code behind:

    publicMainPage()

            {

                InitializeComponent();

                this.wb.LoadCompleted += wb_LoadCompleted;

            }

            voidwb_LoadCompleted(objectsender, NavigationEventArgse)

            {

                varreturnValue = wb.InvokeScript("HelloAlert","Alan Yao");

            }

    • Screenshot:

    In windows phone 8.1, if you're using JavaScript to build the app, then it is not needed to save static html pages to local storage and display them into some controls, instead you could simply display the pages inside your app directly and add whatever script you want. You may want to refer to http://blogs.msdn.com/b/thunbrynt/archive/2014/04/14/windows-phone-8-1-for-developers-for-the-html-javascript-developer.aspx for more information.

    Best Regards,

    Ming Xu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 18, 2014 3:13 PM