Bug Tracker

Modify

Ticket #10603 (closed bug: invalid)

Opened 2 years ago

Last modified 18 months ago

Cannot fill script tag in IE: "Unexpected call to method or property access."

Reported by: petter@… Owned by:
Priority: low Milestone: None
Component: manipulation Version: 1.6.4
Keywords: Cc:
Blocking: Blocked by:

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

comment:1 Changed 2 years ago by petter@…

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

 http://jsfiddle.net/c4tE7/

comment:2 Changed 2 years ago by q.dervish@…

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 2 years ago by timmywil

  • Priority changed from undecided to low
  • Resolution set to invalid
  • Status changed from new to closed
  • Component changed from unfiled to manipulation

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:4 Changed 2 years ago by sindresorhus

#11383 is a duplicate of this ticket.

comment:5 in reply to: ↑ 3 Changed 22 months ago by anonymous

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 18 months ago by beradrian@…

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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as closed
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.