简体   繁体   English

Javascript如何调用外部文件的功能?

[英]how Javascript call to a function of a external file?

i made a html page with this coding 我用这个编码制作了一个html页面

<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 在a.js中,我有此代码

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. 单个<script>标记可以使用src属性链接到外部资源,也可以包含内联JavaScript代码,但是不能同时使用两者。 If you specify a src attribute any content between the <script src="foo.js"> tag and the </script> tag is ignored. 如果指定src属性,则会忽略<script src="foo.js">标记和</script>标记之间的任何内容。

Since you want to load the external JS file, and then execute some JavaScript code, you'll need two separate tags to do so: 由于要加载外部JS文件,然后执行一些JavaScript代码,因此需要两个单独的标记:

<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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM