#10603 closed bug (invalid)
Cannot fill script tag in IE: "Unexpected call to method or property access."
Reported by: | 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>
Change History (6)
comment:1 Changed 11 years ago by
comment:2 Changed 11 years ago by
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.
comment:3 follow-up: 5 Changed 11 years ago by
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.
comment:5 Changed 11 years ago by
Replying to 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...
comment:6 Changed 10 years ago by
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/