Skip to content

Problem with the context when loading a script in an iframe #1757

@mgol

Description

@mgol

Originally reported by jjgcastela@… at: http://bugs.jquery.com/ticket/14568

When you create a script node and append it to a head or a body of an iframe, the code is evaluated in the main window (where the jQuery was loaded) instead of in the window of the iframe. I have been able to solve it adding the desired 'document' as the second argument of the 'globalEval' function this way:

globalEval: function( code , doc) {
    var script,
    indirect = eval;
    code = jQuery.trim( code );
    if ( code ) {
        // If the code includes a valid, prologue position
        // strict mode pragma, execute code by injecting a
        // script tag into the document.
        if ((doc && doc != document) || code.indexOf("use strict") === 1 ) {
            if(!doc) doc = document;
            script = doc.createElement("script");
            script.text = code;
            doc.head.appendChild( script ).parentNode.removeChild( script );
        } else {
            // Otherwise, avoid the DOM node creation, insertion
            // and removal by using an indirect global eval
            indirect( code );
        }
    }
}

And then in the 'domManip' function, adding this parameter in the call to 'globalEval':

jQuery.globalEval( node.textContent.replace( rcleanScript, "" ), doc );

Issue reported for jQuery 2.0.3

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions