We know that in HTML, using <script language= "javascript" type= "Text/javascript" src= "./script.js" ></script> The introduction of the two JS is not possible to call one another. So how do we fix it? Of course, you can copy the code all over, maybe you don't like it.
For example, there is an HTML that has a button that invokes method B () in the B.js file when it is pressed. and B () calls method A () in the A.js file again. To implement this functionality, it is important to note that the JS file code that will be introduced is placed </body>
below.
First, we introduce b.js in HTML and add a reference statement after </body>. As follows:
?
1234567 |
<
html
>
<
body
>
<
input type
=
"button" value
=
"ok" onclick
=
"javascript:b()"
>
</
body
>
<!--这里引用要放在body下面-->
<
script language
=
"JAVASCRIPT" src
=
‘b.js‘
></
script
>
</
html
>
|
The B.js file is as follows:
?
1234567 |
new_element=document.createelement ( New_element.setattribute ( "Text/javascript" New_element.setattribute ( "A.js" //introduced A.js here. Document.body.appendChild (new_element); function b () { A (); |
In the first 4 lines of code in the B.js file we introduced the A.js file and called the A () method in the A.js code in the 7th line of code.
Let's analyze the key code: first, we use the document.createElement("script")
tag that generated a script, set its Type property to Text/javascript,src to A.js (where a.js is placed in the same directory as B.js, and can be placed in a different directory). Finally, the tag is dynamically added to the body. As a result, we can invoke the methods in different JS files.
Supplement (Detailed)
method One, add the following example code at the top of the call file:
?
1 |
document.write(”<script language=javascript src= ‘/js/2.js‘ ><\/script>”); |
Method Two: Put the following code into the body:
?
1234 |
new_element=document.createElement(”script”); new_element.setAttribute(”type”,”text/javascript”); new_element.setAttribute(”src”,”2.js”); document.body.appendChild(new_element); |
Let's take a look at some key lines of code:
First, we use Document.createelement ("script") to generate a script tag, set its Type property to Text/javascript,src to 2.js ( Here the 1.js with 2.js in the same directory, also can be placed in a different directory. Finally, the tag is dynamically added to the body. As a result, we can invoke the methods in different JS files.
Note: <script language= "JAVASCRIPT" src= ' 1.js ' ></script> must be placed under body.
Because the body is used in 2.js (Document.body.appendChild (new_element);)
If the code, such as 2.js, is placed above the body, i.e.,
After entering the page, has not generated the body has executed b.js in the Document.body.appendChild (new_element);
When the body does not exist, it will throw JavaScript errors.
Well, the above is a small compilation for everyone to organize a JS file function called another JS file function of the content of the method. Hope that everyone's learning has helped, but also hope that you continue to support the script house ~
Original link: http://www.jb51.net/article/121051.htm
In VS, let a JS file smart tip Another JS file member 2--specific reference