Geeks With Blogs


Google My Blog

Catch me at: The List!

My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Chris G. Williams Beware: I mix tech and personal interests here.

I'm running into some bizarre behavior with JavaScript in Mobile IE 6. 

I have an ASP.NET MVC 3 Web App, designed primarily for Mobile IE6 (WinMo 6.5 devices)

I have 2 Session variables. The first one simply contains a "0" or "1" and I can access it by doing something like this in JavaScript: 

var lbType = '<%= HttpContext.Current.Session["myVar"] %>'; 

I can use it, display it in an alert, whatever... that works, no problem. 

The other session variable is also a string, but is a bit longer (roughly 250 characters) and has some embedded \r\n (end of line characters). If I use basically the exact same code in JavaScript: 

var lbData = '<%= HttpContext.Current.Session["myData"] %>'; 

It kills the Javascript and the rest of the page renders without executing the JavaScript. I can't work with it or display it in an alert. 

Now, extra weirdness... I copied the string that I'm generating server side and sticking in the session, and pasted it into a local JavaScript variable, like so: 

var lbData = 'lots of data\r\nmore data\r\nmoredata\r\nmore data\r\n'; 

and it works fine. I can work with it, show it in an alert, whatever I need to do. 

The problem is, I can't hardcode the string. I need to generate it serverside and feed it to the javascript. 

Any thoughts? 

p.s. I have also successfully displayed the contents of the Session variable on the page by injecting it into the HTML (but that doesn't do me any good other than seeing it)

Posted on Tuesday, May 24, 2011 1:06 PM | Back to top

Comments on this post: Mobile IE 6 Javascript weirdness

# re: Mobile IE 6 Javascript weirdness
Requesting Gravatar...
Assume you have the following:

var test1 = 'Lorem ipsum\r\nThis is awesome';
var test2 = '<%= "Lorem ipsum\r\nThis is awesome" %>';

The actual HTML source output by this is:

var test1 = 'Lorem ipsum\r\nThis is awesome';
var test2 = 'Lorem ipsum
This is awesome';

You can see that the line break is actually output as a break in the HTML source mid string, causing a script error. Take a look at this blog post:

Left by dadoo Games on May 24, 2011 1:38 PM

# re: Mobile IE 6 Javascript weirdness
Requesting Gravatar...
Thanks!! Jason Bock and I came to a similar answer... Ultimately adding an extra \ before each \r and \n in between the directive tags (<% %>) solved the problem.
Left by Chris G. Williams on May 24, 2011 1:48 PM

# re: Mobile IE 6 Javascript weirdness
Requesting Gravatar...
Now, that is something really weird. Have not experienced the same thing anyway. - Bath Planet Complaints
Left by Robin Edward on Feb 15, 2017 5:29 AM

Your comment:
 (will show your gravatar)

Copyright © Chris G. Williams | Powered by: