简体   繁体   中英

Not able to call a .js file dynamically from some HTML page in some browsers

I call a .js file from my html page like this

var value1 = "Hello";
var value2 = "John";

var oHead1 = document.getElementsByTagName('HEAD').item(0);
var paramScript = document.createElement("script");

paramScript.type = "text/javascript";

        paramScript.setAttribute('value1',
                value1);
        paramScript.setAttribute('value2',
                value2);

oHead1.appendChild(paramScript);

var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript = document.createElement("script");
oScript.type = "text/javascript";
oScript.src = "some.js";
oHead.appendChild(oScript);

This works well in Android and iPhone browser. but it doesn't work in Blackberry OS 5.0 browser

Is there an alternative to this which will work in all browsers?

Instead of setting an empty script tag with attributes set to the variables, it would make more sense just to use the variables set at the beginning of the script...

var value1 = "Hello";
var value2 = "John";

This will not only reduce how many DOM manipulations you are incurring, but should also simplify the way you retrieve the variables in some.js.

One more suggestion I can offer is to wrap your JS in a self-calling function.

(function(){
    // Code Here
}());

You should use a library loader like LAB or Frame . There are a lot of browser peculiarities with script loading and the libraries seem to have them all worked out, though I can't speak to Blackberry OS 5.0 specifically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM