Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mgol opened this issue Oct 21, 2014 · 2 comments

Comments

@mgol
Copy link
Member

commented Oct 21, 2014

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

@mgol

This comment has been minimized.

Copy link
Member Author

commented Oct 21, 2014

Comment author: dmethvin

This will be affected by the switch to script tags being contemplated in  https://github.com/jquery/jquery/pull/1449 .

@mgol mgol added this to the 3.0.0 milestone Oct 21, 2014
@peyerluk

This comment has been minimized.

Copy link

commented Jan 27, 2015

Here's an updated version of the current globalEval with a second param context analogouous to jQuery.parseHTML():

globalEval: function( code, context ) {
  context = context || document;
  var script = context.createElement( "script" );

  script.text = code;
  context.head.appendChild( script ).parentNode.removeChild( script );
}
@timmywil timmywil added the Core label Feb 1, 2015
@timmywil timmywil self-assigned this May 5, 2015
timmywil added a commit to timmywil/jquery that referenced this issue Nov 6, 2015
timmywil added a commit to timmywil/jquery that referenced this issue Nov 9, 2015
@timmywil timmywil closed this in 22449eb Nov 9, 2015
@masone masone referenced this issue Jan 10, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
4 participants
You can’t perform that action at this time.