Skip to main content

Bug Tracker

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 petter@petterhaggholm.net comment:1

Apologies for the inline code (missed the jsFiddle) guideline.

http://jsfiddle.net/c4tE7/

Changed October 28, 2011 08:24PM UTC by q.dervish@gmail.com 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 timmywil comment:3

component: unfiledmanipulation
priority: undecidedlow
resolution: → invalid
status: newclosed

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 February 24, 2012 09:26AM UTC by sindresorhus comment:4

#11383 is a duplicate of this ticket.

Changed July 11, 2012 09:47PM UTC by anonymous 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 beradrian@yahoo.com 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.