Toolbox
  • Printable version
 
Toolbox
LANGUAGES
Language
Personal tools
Wikipedia Affiliate Button

Talk:Loading script tags via AJAX

From BrightByte

Jump to: navigation, search

this works great! Does it work in all browsers that support ajax?

To be honest, I don't remember where I tried it. It works at least in firefox, and I would expect it to work with any ajax capable browser, i.e. anything relatively recent and wide spread. Please share your experience :) -- Daniel 10:36, 13 February 2008 (CET)

[edit] o54SnF Thank you ever so for you post.Thanks Again. Will read on...

o54SnF Thank you ever so for you post.Thanks Again. Will read on...

[edit] Cris Mooney: Thank you, great start for me. Cross frame addition below.

Old post is still relevant, in case anyone ends up here (referenced from a number of other web pages).

For anyone cross scripting, the eval might be done in the wrong scope which could cause you some pain. I have library code in one frame, and dynamically load HTML into a div in another frame using the library. In such a case the script eval would happen in the wrong frame scope, which would effect you if you rely on global script. Consider "onclick=myJS()".

Here is another version based on additions from Google libs that indicate it should work in all but Safari 2 (see ArticleParentWindow link below, I did not want to pay for all the extra code).

// assinging HTML to "div.innerHTML" does not eval "script" blocks. So, we do it manually.
function DOMNodeEvalScripts(n,s) { // n is DOM node (div). s is "scope" (where to eval), for internal use - don't pass it.
	// Since s is already scoped local, we can reuse itself while finding our containing "window" scope via any defined n.document.defaultView, n.document.parentWindow, n.ownerDocument.defaultView or n.ownerDocument.parentWindow.
	if (!s) s=((s=n.document?n.document:n.ownerDocument).defaultView?s.defaultView:s.parentWindow);
	if (n.nodeType == 1) {
		if (n.tagName.toLowerCase() == 'script') s.eval(n.text);
		else for (n = n.firstChild; n; n = n.nextSibling) if (n.nodeType == 1) DOMNodeEvalScripts(n,s);
	}
}

DOMNodeEvalScripts(this.subFrame.document.getElementById("myDIV"));

References:

  http://code.google.com/p/doctype-mirror/wiki/ArticleOwnerDocument
  http://code.google.com/p/doctype-mirror/wiki/ArticleParentWindow

Note: had to hand edit this entry using "edit talk" since "add comment" did not seem to work in Firefox or IE.

-- Cris Mooney 14:35, 26 April 2012 (CET)