DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/)
-   JavaScript / DHTML / AJAX (http://www.daniweb.com/forums/forum117.html)
-   -   Forcing a script to load in Safari -- help needed (http://www.daniweb.com/forums/thread27940.html)

arstein Jul 11th, 2005 8:35 pm
Forcing a script to load in Safari -- help needed
 
Hi,

I've been beating my head against my inability to force Safari to dynamicall
y load an external Javascript file, and was wondering if anyone has any advi
ce. My current code works fine in Firefox and IE:

var e = document.createElement("script");

e.src = url;
e.type="text/javascript";
document.getElementsByTagName("head")[0].appendChild(e);

I want the script to load and then execute. Any suggestions?

alpha_foobar Jul 17th, 2005 9:47 pm
Re: Forcing a script to load in Safari -- help needed
 
I am afraid that I do not have a safari browser to test on. However, I did find, when testing with IE and Firefox, that I had to wait some time for some scripts to be loaded.

I used this code *similar* to this to load script files and check the function I want to load exists... sometimes it can take a few iterations before the function is available to the web page.

function importLibFile(file){
        var e = document.createElement("script");
        e.src = file;
        e.type="text/javascript";
        document.getElementsByTagName("head")[0].insertBefore(e, document.getElementsByTagName("script")[0]);
}

function loadPackage(func, libFile){
        if(document.libraries && !document.libraries[libFile]){
                document.libraries[libFile] = true;
                importLibFile(libFile);
        }
        try {
                func(null, function(s){alert(s);});
                document.getElementsByTagName("body").item(0).innerHTML += (libFile + " package loaded." +"<br/>");
        } catch(exception) {
                document.getElementsByTagName("body").item(0).innerHTML += ("Loading package " + libFile +"<br/>");
                // while appfunction doesn't load
                setTimeout(function(){ loadPackage(func, libFile); }, 250);
        }
}


All times are GMT -4. The time now is 2:23 am.

Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC