Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript
|
Javascript General Javascript discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 20th, 2009, 08:15 PM
Authorized User
 
Join Date: Aug 2009
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
Default dynamically loaded scripts

This is a post which has torture the community very much.

I have AJAX loaded XHTML with scripts:
- External, as <script src="..." />
- Internal, as <script>alert(1);</script>
- As action on events: <a href="" onclick="alert(5); return false;">whow!</a>

IE is not W3C compliant browser so I don't care to support it.

Opera works without any additional work. Scripts running when AJAX loading completes. :)

Firefox works:
- On external with code below (create a <script> element and append on <head> element) :eek:
- On internal with code below (eval) :eek:
- As action on events, without any additional work. :)

Webkit (Safari/Chrome) works:
- On external with code below (create a <script> element and append on <head> element) :eek:
- On internal with code below (eval) :eek:
- As action on events, doesn't work at all :(

So, these questions:
- What I can do to eval() element's event's script on Webkit (Safari/Chrome)? (like <a href="" onclick="alert(5); return false;">whow!</a>)
- Is there a schedule for Firefox and Webkit to automatically load and eval dynamically loaded scripts with AJAX, exactly like Opera?

the code: (innerDOM is the function)
Code:
// create element 'tag'
// add attributes from 'attr' array: [ [ 'attribute1', 'value1' ], [ 'attribute2', 'value2' ] ]
// add contents from 'content' array: [ 'a string', ELEMENT, 5 ]
// append it to 'owner' element
function createTag(tag, content, owner, attr) {
	var t = document.createElement(tag);
	if (attr instanceof Array)
		for (var z = 0; z < attr.length; z++)
			t.setAttribute(attr[z][0], attr[z][1]);
	if (content instanceof Array)
		for(var z = 0; z != content.length; z++)
			if (typeof(content[z]) == 'string' || typeof(content[z]) == 'number')
				t.appendChild(document.createTextNode(content[z]));
			else t.appendChild(content[z]);
	if (owner) owner.appendChild(t);
	return t;
}

// insert 'root' xml tree to 'where' element
function innerDOM(where, root) {
	for (var z = 0; z < root.childNodes.length; z++)
		where.appendChild(root.childNodes[z].cloneNode(true));

	var scripts = where.getElementsByTagName("script");
	for (v in scripts) {
		if (scripts[v].getAttribute('src'))
			createTag('script', null, document.getElementsByTagName("head")[0], [['type', 'text/javascript'], ['src', scripts[v].getAttribute('src')]]);
		else eval(scripts[v].text);
	}
}

Last edited by chameleon; September 20th, 2009 at 08:18 PM..
 
Old September 28th, 2009, 03:05 PM
Authorized User
 
Join Date: Aug 2009
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Ok, it is a known Webkit bug.
 
Old January 14th, 2010, 08:58 AM
Registered User
 
Join Date: Mar 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Check this url for help: www.codingdeveloper.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically changing the <object> element's loaded content codeexploiter BOOK: Professional JavaScript for Web Developers 2nd edition ISBN: 9780470227800 3 October 5th, 2009 08:14 AM
Problem with dynamically loaded user control CodeBest ASP.NET 2.0 Basics 0 February 7th, 2009 09:42 AM
Page Loaded in Seconds Scripts hcweb Classic ASP Basics 8 February 25th, 2004 09:58 PM
How can i know that a form is loaded. sharma_atin VB How-To 1 September 1st, 2003 10:51 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.