Side navigation
#10603 closed bug (invalid)
Opened October 27, 2011 07:17PM UTC
Closed November 01, 2011 11:56PM UTC
Last modified November 01, 2012 04:09PM UTC
Cannot fill script tag in IE: "Unexpected call to method or property access."
Reported by: | petter@petterhaggholm.net | Owned by: | |
---|---|---|---|
Priority: | low | Milestone: | None |
Component: | manipulation | Version: | 1.6.4 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description
Attempting to fill a script tag using either $.text() or $.html() works fine in Firefox and, I’m told, in IE9, but fails in IE7 and IE8. An illustrative example: The same sort of thing works for a paragraph but fails for a script tag, yielding the error message
“Unexpected call to method or property access.”
<html> <head> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> //<![CDATA[ $(document).ready(function(){ $('body').append( $('<p/>').text('This should work, but the alert fails in IE<9') ); $('head').append( $('<script/>', {'type': 'text/javascript'}).text('alert("foo");') ); }); //]]> </script> </head> <body> </body>
Attachments (0)
Change History (6)
Changed October 27, 2011 07:23PM UTC by comment:1
Changed October 28, 2011 08:24PM UTC by comment:2
I encountered the same bug.
The problem is that jQuery.fn.text function appends a child text node to the script (line 5746), but in IE8, the script node does not support child nodes. The workaround is to set the text property of the script node.
Changed November 01, 2011 11:56PM UTC by comment:3
component: | unfiled → manipulation |
---|---|
priority: | undecided → low |
resolution: | → invalid |
status: | new → closed |
There are much better ways to insert scripts and execute code. See http://api.jquery.com/jQuery.getScript to insert an external script. I can't imagine a valid use case for inserting inline scripts.
Changed July 11, 2012 09:47PM UTC by comment:5
Replying to [comment:3 timmywil]:
There are much better ways to insert scripts and execute code. See http://api.jquery.com/jQuery.getScript to insert an external script. I can't imagine a valid use case for inserting inline scripts.
Wow that is an amazingily short sited comment. You can't think of a valid use case? I don't even know where to start...
Changed November 01, 2012 04:09PM UTC by comment:6
In IE7/8 you should use the text property. I think the bug is still valid, because you can use the SCRIPT tag for embedding templates, not necessarily JavaScripts.
Apologies for the inline code (missed the jsFiddle) guideline.
http://jsfiddle.net/c4tE7/