As I distribute many JS functions to various files, one JS function needs another JS function to run. One solution is to append a block in the runtime by appending another Js in JS. The Code is as follows:
new_element = document. createelement ("script");
new_element.setattribute ("type", "text/JavaScript");
new_element.setattribute ("src", "another. JS ");
document. body. appendchild (new_element);
function B1 ()
{< br> A1 ("B1");
}< br> the content of another JS file is as follows:
function A1 (SINFO)
{< br> alert ("A1-" + SINFO);
}
however, this method has the following problems:
· The Declaration referenced by the JS file must be placed after
cannot be written like this
but only
later, a solution was found, handle the onload event in the body
......
In b1.js, write the sub-Program loadjs4b1:
function loadjs4b1 ()
{< br> new_element = document. createelement ("script");
new_element.setattribute ("type", "text/JavaScript");
new_element.setattribute ("src", "a1.js ");
document. body. appendchild (new_element);
}
Finally, I found that after half a day, it is not as simple and intuitive as follows:
Simply add this sentence to b1.js, and do not perform operations such as new_element or appendchild, nor subprograms, or set onload to process code in the body.
Document. Write ('<SCRIPT type = "text/JavaScript" src = "a1.js"> </' + 'script> ');
After testing, it is feasible not to split the script.
Document. Write ("<SCRIPT type = \" text/JavaScript \ "src = \" a1.js \ "> </SCRIPT> ");
· Directly running another JS function in the JS file will not work, but it will be necessary to wait until the page is fully loaded.
Code like this in JS fails.
Function B1 ()
{
A1 ("B1 ");
}
B1 ();
And such code in HTML is enough.
<Input type = button value = OK onclick = "B1 ();"/>
You can use the above method: Execute in body onload, which may be no difference.