Script Tags, jQuery, And Html(), Text() And Contents()

Posted January 26, 2010 at 2:00 PM by Ben Nadel

Tags: Javascript / DHTML

The other day, Matt Olson left a comment on my jQuery and ColdFusion rating system post that the jQuery script I had was not working in Internet Explorer (IE). I confirmed this on my local copy and eventually narrowed it down to a discrepancy in the way IE can access the contents of a non-Javascript Script tag. Originally, I had been using the text() method to extract the Script contents, which worked fine in FireFox. In IE, however, I had to switch over to the html() method.

After this discovery, I figured I would run the various jQuery script-content-access methods in IE and FireFox to see what else I might discover. Here is the test code that I ran:

  • <!DOCTYPE HTML>
  • <html>
  • <head>
  • <title>jQuery: Scripts Tags And Text(), HTML(), and Contents()</title>
  • <script type="text/javascript" src="jquery-1.4.js"></script>
  • <script type="text/javascript">
  •  
  • // When the DOM is ready, init scripts.
  • jQuery(function( $ ){
  •  
  • // Get a reference to our Script tag.
  • var script = $( "#data" );
  •  
  • // Get a reference to our output tags.
  • var textOutput = $( "#text-output" );
  • var htmlOutput = $( "#html-output" );
  • var contentsTextOutput = $( "#contents-text-output" );
  • var contentsHtmlOutput = $( "#contents-html-output" );
  •  
  • // Use the various approachs to output the contents
  • // of the Script tag to the document.
  •  
  • // Text.
  • textOutput.text( script.text() );
  •  
  • // Html.
  • htmlOutput.text( script.html() );
  •  
  • // Contents - Text.
  • contentsTextOutput.text( script.contents().text() );
  •  
  • // Contents - Html.
  • contentsHtmlOutput.text( script.contents().html() );
  •  
  • });
  •  
  • </script>
  • </head>
  • <body>
  •  
  • <h1>
  • jQuery: Scripts Tags And Text(), HTML(), and Contents()
  • </h1>
  •  
  • <script id="data" type="application/x-json">
  • {script-content}
  • </script>
  •  
  • <!-- This is where the contents of script will be output. -->
  • <p>
  • TEXT: <span id="text-output"> ... </span>
  • </p>
  •  
  • <p>
  • HTML: <span id="html-output"> ... </span>
  • </p>
  •  
  • <p>
  • CONTENTS (TEXT): <span id="contents-text-output"> ... </span>
  • </p>
  •  
  • <p>
  • CONTENTS (HTML): <span id="contents-html-output"> ... </span>
  • </p>
  •  
  • </body>
  • </html>

As you can see, I have a Script tag with some non-Javascript content. Then, I use the various methods - text(), html() and contents() - to try an access that content and output it to the screen. I tested this on IE6, IE7, IE8, FireFox, Safari, and Chrome. The non-IE browsers were all the same. The IE browsers were all the same which one minor exception: IE6 and IE7 added additional white space that IE8 did not.

IE Browsers (IE6, IE7, ~IE8)

 
 
 
 
 
 
Accessing Script Tag Content With jQuery In IE6, IE7, and IE8. 
 
 
 

Non-IE Browsers (FireFox, Safari, Chrome)

 
 
 
 
 
 
Accessing Script-Tag Content With jQuery And FireFox. 
 
 
 

As you can see, IE was only able to access the Script tag contents using the html() method. All non-IE browsers, on the other hand, were able to use text(), html(), and contents(). None of the browsers, however, were able to access the content using contents().html(). This is not a limitation of the browser or jQuery, but rather a byproduct of the fact that there are no non-text DOM nodes contained within the Script tag.

I cannot figure out why there is extra white space in the IE-based output when it grabs the content via html(). It's as if it is rendering the content using a PRE tag or something (minus the line breaks). That must just be a bug - one that has been fixed in IE8.

All in all, it looks like the html() method is the only cross-browser safe way to access the contents of a Script tag. That's good to know, especially since Script tags act as a very convenient container for HTML templates.



Reader Comments

Feb 12, 2011 at 9:09 AM // reply »
1 Comments

Thank for this post


Feb 12, 2011 at 11:14 AM // reply »
10,743 Comments

@Jeune,

I'm glad you found this helpful.


Apr 12, 2011 at 4:25 PM // reply »
1 Comments

Thanks for the informative post!
I'm looking into using mustache.js to do client side templated html using the script tag to hold the templates, and I would like to keep the templates in a separate HTML file and load that in the script tag through the src attribute, but that does not seem to work. I'm getting to think this is not possible; have you tried to embed HTML templates from a separate file using the script tag, or have other suggestions to achieve this?


Jan
Aug 19, 2011 at 7:34 AM // reply »
3 Comments

Thanks for your tests. I was really wondering why I couldnīt get my script-content with .text() in IE. Now with .html() itīs working fine in all tested browsers.


Aug 22, 2011 at 3:50 PM // reply »
2 Comments

Is it possible to access the json content in the case of a remote src url, as in <script id="data" type="application/x-json" src="<some feed url>">? I can't quite get such a thing to work and was hoping to find a way around xss restrictions...


Jan
Aug 23, 2011 at 2:38 AM // reply »
3 Comments

@Sam: Why don't you use Ajax to read the feed?


Sep 6, 2011 at 8:15 PM // reply »
2 Comments

@Jan I don't believe you can access a feed that isn't in the same domain as the page you're on via jQuery.get(). I ended up using JSONP with yahoo pipes, using document.write("<script ....>") and then as a url parameter I indicate the javascript function to pass the data to once the script loads the data. Handy way to get around XSS issues since there's no equivalent of Flash's crossdomain.xml (or is there?)


Jan
Sep 7, 2011 at 4:12 AM // reply »
3 Comments

How about using a local Ajax-Call to a php file, which reads the remote feed for you? I think something like this should be possible if allow_url_fopen is true in your php.ini.

http://de.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen

http://de.php.net/manual/en/function.file-get-contents.php


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 16, 2012 at 8:18 PM
Best Of ColdFusion 10 Contest Entry - HTML Email Utility
Just found this, looks good! I'm trying to run it on local, it's the 64bit version and I'm experiencing horrible lag. On average the generate.cfm processes the content change in 60-90 seconds. I've ... read »
May 16, 2012 at 6:40 PM
Maintaining Sessions Across Multiple ColdFusion CFHttp Requests
I am trying to integrate this CFHTTPsession into an application that will log into zeekrewards.com to post ads and I am not having any luck. The code works perfectly for logging into other websites, ... read »
May 16, 2012 at 2:44 PM
Creating A Sometimes-Fixed-Position Element With jQuery
Thank you, very useful technique! Worked like a charm. ... read »
May 16, 2012 at 1:58 PM
Movies As A Religious Experience
Acting can, in a way, ruin the movie-goer's experience. I used to be able to get so caught up in movies and their plots, and totally engaged. But lately, I haven't been able to as much with a lot o ... read »
May 16, 2012 at 1:52 PM
The Science Of Optimal Post-Exercise Nutrition
children of this age eat very less vegetables so u can opt for salads they will like it also carrot ,cucumber,onion and as far as pulses are concerned u can boil them ,give him along with mashed rice ... read »
May 16, 2012 at 1:34 PM
Strange ColdFusion JRUN Stack Overflow Error
Hey, Recently I updated my jrun4 using the latest updater 7 and now i am having memory issues :(:(:( any help is appreciated ... read »
May 16, 2012 at 9:56 AM
ColdFusion 10 Beta, Apache Tomcat, And Symbolic Links On Mac OSX
Hi, Now that ColdFusion 10 is out I have stumbled over this as well and I cannot figure out the proper solution. We're running virtual hosts via Apache2; the ColdFusion-applications store their fil ... read »
May 15, 2012 at 6:03 PM
Movies As A Religious Experience
@Ben, I don't know whether you'd consider this a religious observation, but it seems to me, in a sense, movies multiply how many lives we get to have. Each movie is like a little extra life we get ... read »