Click here to Skip to main content
Click here to Skip to main content
Go to top

WPF WebBrowser Control and Javascript

, 15 Jun 2012
Rate this:
Please Sign up or sign in to vote.
One way to get around the "invalid character" error

Arggggghhh!

I was working on a WPF app in which we're converting XAML "scenes" to HTML. This means we're building the HTML in memory. One of the requirements of the conversion dictates the use of javascript in the rendered HTML scene. The scripts we're using (jQuery and some jQuery code) are fairly lengthy, and I was trying to do this in the HTML:

StringBuilder html = new StringBuilder();
...
string jqueryPath = System.IO.Path.Combine(appPath, "jquery-1.7.2.js"); 
html.AppendFormat("<script type=\"text/javascript\" src=\"{0}\"; ></script>", jqueryPath);

Pretty standard stuff, right? Well, actually, no.

When I navigated to my html stream, I received the error "invalid character at line 1 character 1". If I saved the stream to a HTML file, and loaded it in IE (v9), it worked fine (no error).

The problem is related to security for locally loaded files (if you want to google it, be my guest), and there are three fixes available (that I could find):

  • Add "about:blank" to your Trusted Sites in IE (we couldn't do this because it added one more moving part to the deployment process of the app, and probably compromises security in some as-yet-to-be-determined manner.

  • Create a blank HTML file, load it, and modify the DOM by inserting script elements programatically . We didn't want to do this because there was a possibility we'd forget to add something, and this code also has to eventually be cross-platform.

  • Just bite the bullet and add the script file content as text in the string. This meant we'd have to load the file ourselves, and insert it into the StringBuilder object.

We chose the third option. It was the easiest and posed the fewest possible problems. Your mileage may vary.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
GeneralTip PinmemberPhilDs16-Nov-12 22:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 15 Jun 2012
Article Copyright 2012 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid