load additional javasscripts via loadPage function when using AJAX

load additional javasscripts via loadPage function when using AJAX

When using AJAX to transition pages, no additional javascripts are loaded if the new page has new javascript in the header, etc..

I have done a dirty hack to the loadPage function which loads all the additional scripts from the html string returned via AJAX (see attached for hacked version).
The code just needs adding to the success function of the ajax call in the loadPage function-



                                        // START modification (Guy Thomas 2012083000)
                                        // add scripts to head and body
                                        var myheadscripts=document.getElementsByTagName("head")[0].getElementsByTagName("script");
                                        var mybodyscripts=document.getElementsByTagName("body")[0].getElementsByTagName("script");
                                        var el=document.createElement('div');
                                        var bogustags=['head','body','script','link'];
                                        for (var b in bogustags){
                                            var bogustag=bogustags[b];
                                            html=html.replace(new RegExp('<'+bogustag,'g'), '<'+bogustag+'_bogus');
                                            html=html.replace(new RegExp('</'+bogustag,'g'), '</'+bogustag+'_bogus');
                                            html=html.replace(new RegExp('</ '+bogustag,'g'), '</ '+bogustag+'_bogus');
                                        }
                                                                 
                                       
                                        el.innerHTML=html;
                                        document.body.appendChild(el);
                                        var head=el.getElementsByTagName('head_bogus')[0];
                                        var scripts=head.getElementsByTagName('script_bogus');
                                        for (var s in scripts){
                                            var script=scripts[s];
                                            var ssrc='';
                                            var stype='';
                                            if (typeof(script.getAttribute)!='undefined'){
                                                ssrc=script.getAttribute('src');
                                                stype=script.getAttribute('type');
                                            }
                                            var hscript=document.createElement('script');
                                            var alreadypresent=false;
                                            if (ssrc!=''){                                                                                               
                                                for (var hs in myheadscripts){
                                                    var headscript=myheadscripts[hs];
                                                    if (typeof(headscript.getAttribute)!='undefined'){
                                                        if (headscript.getAttribute('src')==ssrc){
                                                            alreadypresent=true;
                                                            break;
                                                        }
                                                    }
                                                }                                               
                                                hscript.src=ssrc;
                                                hscript.type=stype;
                                            } else {   
                                                for (var hs in myheadscripts){
                                                    var headscript=myheadscripts[hs];
                                                    if (headscript.innerHTML==script.innerHTML){
                                                            alreadypresent=true;
                                                            break;
                                                    }
                                                   
                                                }
                                                hscript.innerHTML=script.innerHTML;
                                            }
                                            if (alreadypresent==false){
                                                document.getElementsByTagName("head")[0].appendChild(hscript);
                                            }
                                        }
                                       
                                        var body=el.getElementsByTagName('body_bogus')[0];
                                        var scripts=body.getElementsByTagName('script_bogus');
                                        for (var s in scripts){
                                            var script=scripts[s];
                                            var ssrc='';
                                            var stype='';
                                            if (typeof(script.getAttribute)!='undefined'){
                                                ssrc=script.getAttribute('src');
                                                stype=script.getAttribute('type');
                                            }
                                            var bscript=document.createElement('script');
                                            var alreadypresent=false;
                                            if (ssrc!=''){                                                                                               
                                                for (var bs in mybodyscripts){
                                                    var bodyscript=mybodyscripts[bs];
                                                    if (typeof(bodyscript.getAttribute)!='undefined'){
                                                        if (bodyscript.getAttribute('src')==ssrc){
                                                            alreadypresent=true;
                                                            break;
                                                        }
                                                    }
                                                }                                               
                                                bscript.src=ssrc;
                                                bscript.type=stype;
                                            } else {   
                                                for (var bs in mybodyscripts){
                                                    var bodyscript=mybodyscripts[bs];
                                                    if (bodyscript.innerHTML==script.innerHTML){
                                                            alreadypresent=true;
                                                            break;
                                                    }
                                                   
                                                }
                                                bscript.innerHTML=script.innerHTML;
                                            }
                                            if (alreadypresent==false){
                                                page.append(bscript);
                                            }
                                        }
                                        el.parentNode.removeChild(el);
                                        // END modification (Guy Thomas 2012083000)