Forum Moderators: open

Message Too Old, No Replies

AJAX vs. adding script to head

Opinions?

         

vincevincevince

2:47 am on Oct 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've realised that you can create a new 'SCRIPT' node and insert it into the DOM via appendChild to the HEAD.

This allows reference to an external SRC as per usual; and executes as soon as it is inserted.

Now; I can see that this is an advantage over AJAX as it doesn't require ActiveX to work in IE; and it just means that your response should be valid Javascript instead of valid XML, e.g. you write your DOM manipulations directly as javascript in your response.

I'd be glad to hear of any 'down side' to this. I imagined that it could lead to eventually overflowing memory due to massive numbers of scripts; but this can be avoided by javascript to delete the script on the last line of its execution - seems to run fine.

whoisgregg

3:15 pm on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a handy approach. The javascript libraries that offer cross-site AJAX support (I believe) all do it through dynamic insert of <script> elements into the <head>.

If you need cross-site, definitely take advantage of that. :)

m0thman

4:14 pm on Oct 31, 2007 (gmt 0)

10+ Year Member



I think this was referred to as header hijacking. It's an old technique, I used it once to create web based chat app before the term AJAX became popular.

vincevincevince

6:01 am on Nov 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So; let me turn it around another way...

... why would one use AJAX instead of this technique, given the better browser support?

vincevincevince

11:50 am on Nov 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Gentle bump on this one, with apologies, as it's quite an important topic for me at the moment:

Why would someone use AJAX instead of adding a new SCRIPT object to the HEAD dynamically, given the much better browser support for the latter method?

whoisgregg

2:31 pm on Nov 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I had to dig to find it, but here's an article displaying the differences [xml.com] in a nice chart. I found it with a google search for "dynamic script tags drawbacks [google.com]" so you might find more there. :)