About the reference of other Js in JS

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.