locked
parent window property for webview control

    Question

  • How can I get parent window from script loaded inside my webview control?
    Thursday, May 15, 2014 6:30 AM

Answers

  • It's by design that Webview-hosted content does not have direct access to its hosting application. As you can imagine, that would open up many security issues because it would allow arbitrary websites to drive arbitrary apps, including those that the app didn't necessarily expect to host given that you can navigate the web in a Webview.

    However, the Webview and the host app can communicate through invokeScriptAsync (app calling Webview) and window.external.notify + the Webview's MSWebViewScriptNotify event (Webview to app). This means you intentionally design a protocol between the app and content it's expecting to load into the Webview, so you retain conscious control over everything that happens.

    In any case, be clear that the app and the Webview have separate and isolated DOM and script contexts. An element from the app won't mean anything inside the DOM of the Webview, which is fundamentally why trying to get to window.parent from the Webview doesn't work.

    Kraig

    Author, Programming Windows Store Apps with HTML, CSS, and JavaScript, Second Edition, a free ebook from Microsoft Press.


    Thursday, May 15, 2014 9:00 PM

All replies

  • It's by design that Webview-hosted content does not have direct access to its hosting application. As you can imagine, that would open up many security issues because it would allow arbitrary websites to drive arbitrary apps, including those that the app didn't necessarily expect to host given that you can navigate the web in a Webview.

    However, the Webview and the host app can communicate through invokeScriptAsync (app calling Webview) and window.external.notify + the Webview's MSWebViewScriptNotify event (Webview to app). This means you intentionally design a protocol between the app and content it's expecting to load into the Webview, so you retain conscious control over everything that happens.

    In any case, be clear that the app and the Webview have separate and isolated DOM and script contexts. An element from the app won't mean anything inside the DOM of the Webview, which is fundamentally why trying to get to window.parent from the Webview doesn't work.

    Kraig

    Author, Programming Windows Store Apps with HTML, CSS, and JavaScript, Second Edition, a free ebook from Microsoft Press.


    Thursday, May 15, 2014 9:00 PM
  • Thanks kraig, but how can I pass one object from the App to one javascript function of webview and use it there.


    Monday, May 19, 2014 10:24 AM