|
|
 |
| Javascript General Javascript discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.
|
 |

September 20th, 2009, 09:15 PM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
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 09:18 PM.
|

September 28th, 2009, 04:05 PM
|
|
Authorized User
|
|
Join Date: Aug 2009
Posts: 11
Thanks: 3
Thanked 0 Times in 0 Posts
|
|
Ok, it is a known Webkit bug.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |