Blogus Maximus

Rubbing people the wrong way since 1970...

  Home  |   Contact  |   Syndication    |   Login
  1366 Posts | 10 Stories | 2225 Comments | 1336 Trackbacks

News


Google My Blog

Catch me at: The List!


My InstallScript Utility Belt My Amazon Wishlist
My Standard Disclaimer

Tag Cloud


Archives

Post Categories

Image Galleries

Blogs

Code Camps

CTown Geeks

Geeky Webcomics

High Geek

Magenic Blogs

Microsoft Blogs

My Articles

My Sites

PodCasts

UG

XNA

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

Feedback

# re: Mobile IE 6 Javascript weirdness 5/24/2011 1:38 PM dadoo Games
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: http://www.west-wind.com/weblog/posts/2007/Jul/14/Embedding-JavaScript-Strings-from-an-ASPNET-Page



# re: Mobile IE 6 Javascript weirdness 5/24/2011 1:48 PM Chris G. Williams
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.

Post A Comment
Title:
Name:
Email:
Comment:
Verification: