Wpf WebBrowser load javascript

Viewed 2650

I have this WebBrowser in my page:

<WebBrowser HorizontalAlignment="Stretch" Name="Browser"
            VerticalAlignment="Stretch" Grid.Column="1"/>

Now after i load a page i want to inject a JavaScript code with :

var doc = (HTMLDocument)Browser.Document;
var head = doc.getElementsByTagName("head").Cast<HTMLHeadElement>().First();
var script = (IHTMLScriptElement)doc.createElement("script");
script.text = MYSCRIPTCODE;
head.appendChild((IHTMLDOMNode)script);

And in MYSCRIPTCODE i have this function:

function checkSomething() {

    try{
           var xhr = new XMLHttpRequest();
           var url = 'weburl';
           xhr.open("GET", url, true);
           xhr.onreadystatechange = function () {
           //Somecode
           }

           xhr.send();
    } catch {

    }
}

And i run it with:

Browser.InvokeScript("checkSomething");

It's work perfect and run the script without any problem.

I have another state that i load local html file to the browser:

Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + @"OfflinePage.html");
Browser.Navigate(uri);

And when i want to run it with :

Browser.InvokeScript("checkSomething");

This line in the script get exception - Permission Denied:

xhr.open("GET", url, true);

Any idea why it happen? why it work in a page that i load from web and won't work in a local html?

1 Answers
Related