Bug Tracker

Opened 10 years ago

Closed 9 years ago

#14568 closed bug (migrated)

Problem with the context when loading a script in an iframe

Reported by: jjgcastela@… Owned by:
Priority: high Milestone: 1.12/2.2
Component: ajax Version: 2.0.3
Keywords: Cc:
Blocked by: Blocking:


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 );

Change History (3)

comment:1 Changed 10 years ago by dmethvin

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

comment:2 Changed 9 years ago by dmethvin

Component: unfiledajax
Milestone: None1.12/2.2
Priority: undecidedhigh
Status: newopen

comment:3 Changed 9 years ago by m_gol

Resolution: migrated
Status: openclosed
Note: See TracTickets for help on using tickets.