George Hernando
George Hernando

Reputation: 2650

include static JS and CSS WebBrowser control

I'm trying to include js and css resource files that are local to my project on an html page loaded as a string into a WPF WebBrowser control.

The project builds a dll for a desktop application. Using Visual Studio 2013 and C#.

The HTML loads in the the WebBrowser, but I get errors related to including the JS file.

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        string page = GetPage();
        if (page != null)
        {
            string resourcePath = System.IO.Path.GetFullPath("Resources/");

            //  Try setting an absolute path
            page = page.Replace("Resources/", resourcePath);
            this.webbrowser.NavigateToString(page);
        }
    }

Embedded within the html string is the following:

<head>
    <script language="JavaScript" src="Resources/myscript.js"></script> 
    <link rel="stylesheet" type="text/css" href="Resources/mystyle.css"> 
</head>

I get an error popup like:

An error has occurred in the script on this page.

URL:  aboutC:\MyDir\MyProject\bin\Debug\Resources/myscript.js

It prepends "about" to the URL. I probably also need to change the slashes. I tried a number of things. "about" is always prepended.

Also tried using IsolatedStorage, but that doesn't work for my project: http://msdn.microsoft.com/library/windows/apps/ff431811(v=vs.105).aspx

Upvotes: 0

Views: 2379

Answers (1)

Mitch
Mitch

Reputation: 22261

You have to use absolute URIs when calling NavigateToString. Since you are not, IE assumes about: as the relative path to cause it to fail.

Either use file:///C:\some\path.css, res://somedll.dll/path.css, or host a HTTP server using WCF on a random high port and use http://localhost:37458/path.css.

Upvotes: 4

Related Questions