JQuery problem getting text from script tag?

I have this little HTML document:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HTML Test</title> <script type="text/javascript" src="jquery-1.3.2.min.js"></script> <script type="text/javascript"> $(document).ready(function() { $("script").each(function() { if($(this).attr("type") == "code") { alert($(this).text()); } }); }); </script> </head> <body> <script type="code"> var Text = "Text"; </script> </body> </html> 

When launched using Firefox, the warning displays the text content of the <script type="code"> . When launched in IE8, it does not display anything.

Do you know why? I'm at a dead end.

+4
source share
3 answers

You may have more luck with .html (), and if that doesn't work, try this.innerHtml . However, I have not tested this.

I have one more tip for your code. If you only need code type scripts, you can have one selector instead of checking the attribute in a loop:

 $("script[type=code]").each(function() { alert($(this).html()); alert(this.innerHtml); }); 
+7
source

Use .html () instead of better support with.

You can also see: http://docs.jquery.com/Plugins/Metadata for storing information in script tags.

+2
source
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HTML Test</title> <script type="text/javascript" src="jquery-1.3.2.min.js"></script <script type="text/javascript"> $(document).ready(function() { $("script").each(function() { if($(this).attr("type") == "code") { alert($(this).html()); } }); }); </script> </head> <body> <script type="code"> var Text = "Text"; </script> </body> </html> 
+1
source

All Articles