To achieve dynamic loading JS script has four kinds of methods:

1, direct document.write

<script language="javascript">

document.write ( "<script src='test.js'> <\ / script>");

</ script>

2, dynamic script to change the src property has been

<script src=''> </ script>

<script language="javascript">

s1.src = "test.js"

</ script>

3, dynamically created script elements

<script>

var oHead = document.getElementsByTagName ( 'HEAD'). item (0);

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

oScript.type = "text / javascript";

oScript.src = "test.js";

oHead.appendChild (oScript);

</ script>

These three methods are asynchronous execution, that is to say, these scripts are loaded at the same time, page the script continues to run, if the above method, then the following code will not receive the desired results.

To dynamic loading of JS script: a.js, the following are the contents of the document.

var str = "Chinese";

alert ( "This is a.js variables:" + str);

Home noodle code:

<script language="JavaScript">

function LoadJS (id, fileUrl)

(

var scriptTag = document.getElementById (id);

var oHead = document.getElementsByTagName ( 'HEAD'). item (0);

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

if (scriptTag) oHead.removeChild (scriptTag);

oScript.id = id;

oScript.type = "text / javascript";

oScript.src = fileUrl;

oHead.appendChild (oScript);

)

LoadJS ( "a.js");

alert ( "page and check dynamic loading a.js one of the variables:" + str);

</ script>
After the above code execution a.js the implementation of the alert and pop-up message,

But produced the wrong page, there is no pop-up dialog box. The reason is that 'str' is not defined, Why? Home noodle in check because when str has not completely loaded a.js success. Encountered in the implementation of necessary synchronization script, they can use the following fourth method.

4, Principle: use XMLHTTP to get the contents of the script, and then create a Script object.

Note: a.js must use UTF8 encoding to preserve, we should not go wrong. Since server and XML to transmit data using the UTF8 encoding.

Home noodle code:

<script language="JavaScript">

function GetHttpRequest ()

(

if (window.XMLHttpRequest) / / Gecko

return new XMLHttpRequest ();

else if (window.ActiveXObject) / / IE

return new ActiveXObject ( "MsXml2.XmlHttp");

)

function AjaxPage (sId, url) (

var oXmlHttp = GetHttpRequest ();

oXmlHttp.OnReadyStateChange = function ()

(

if (oXmlHttp.readyState == 4)

(

if (oXmlHttp.status == 200 | | oXmlHttp.status == 304)

(

IncludeJS (sId, url, oXmlHttp.responseText);

)

else

(

alert ( 'XML request error:' + oXmlHttp.statusText + '(' + oXmlHttp.status + ')');

)

)

)

oXmlHttp.open ( 'GET', url, true);

oXmlHttp.send (null);

)

function IncludeJS (sId, fileUrl, source)

(

if ((source! = null) & & (! document.getElementById (sId))) (

var oHead = document.getElementsByTagName ( 'HEAD'). item (0);

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

oScript.language = "javascript";

oScript.type = "text / javascript";

oScript.id = sId;

oScript.defer = true;

oScript.text = source;

oHead.appendChild (oScript);

)

)

AjaxPage ( "scrA", "b.js");

alert ( "page JS script dynamically loaded.");

alert ( "page and check dynamic loading a.js one of the variables:" + str);

</ script>
Now completed a JS script dynamic loading.

var Rash = true;
var msg = "";
function norash ()
(
if (confirm ( "you sure you want to cancel"))
Rash = false;
)
function rashit ()
(
setInterval ( 'getrss ()', Inttime);
)
function getrss ()
(
if (Rash == true)
(
head = document.getElementsByTagName ( 'head'). item (0);
script = document.createElement ( 'script');
script.src = 'INCLUDE / AutoUpdate.asp';
script.type = 'text / javascript';
script.defer = true;
void (head.appendChild (script));
window.status = msg;
)
)
rashit ();