简体   繁体   中英

how Javascript call to a function of a external file?

i made a html page with this coding

<html>
<head>
<script src="../a.js">
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>
</head>
<body>
</body>
</html>

In a.js i have this code

function m_web(u,i) {
    alert('l');
    alert('u');
}

but my webpage is unable to call this function which is coded in an external file. i am not getting any alert with this. i don't know what is problem. plz tell me simple solution for this. thanx in advance

A single <script> tag can link to an external resource using the src attribute OR contain inline JavaScript code, but it can't do both at the same time. If you specify a src attribute any content between the <script src="foo.js"> tag and the </script> tag is ignored.

Since you want to load the external JS file, and then execute some JavaScript code, you'll need two separate tags to do so:

<script src="../a.js"></script>
<script>
    // your code
</script>

plz write your code like below

<script src="../a.js"></script> </script>
   //^^^^^^^^^^^^^^^^^^ first close script tag of linked js file
<script type="text/javascript">// then call your inline jscode
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>

Try

you are not closing script tag

<script src="../a.js"></script>
                       ^//added closing script tag
<script>
var u=document.URL; var i='t4527878445'; m_web(u,i);
</script>

to alert what you pass use

function m_web(u,i) {
    alert(i);
    alert(u);
}

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