use*_*823 2 javascript events function
我希望从HTML页面调用javascript函数,我不希望它依赖于任何事件.该函数位于单独的.js文件中,因为我希望从许多网页中使用它.我也将变量传递给它.我试过这个:
HTML:
<script type="text/javascript" src="fp_footer2.js">
footerFunction(1_basic_web_page_with_links, 1bwpwl.html);
</script>
Run Code Online (Sandbox Code Playgroud)
fp_footer2.js中的函数:
function footerFunction(path, file) {
document.write("<a href=" + path + "/" + file + " target='_blank'>Link to the original web page for this assignment.</a>");
return;
}
Run Code Online (Sandbox Code Playgroud)
(我也尝试将fp_footer2.js文件引用放在标题中,但无济于事.我不确定我是否可以像在本例中那样把它"内联".如果没有,请告诉我.
PS:我知道我可以用HTML本身的简单'a href =""来做到这一点.为了我自己的好奇心,我想看看这是否可行.
如果a <script>
有a src
,则外部脚本将替换内联脚本.
您需要使用两个脚本元素.
传递给函数的字符串也需要是实际字符串而不是未定义的变量(或未定义变量的属性).必须引用字符串文字.
<script src="fp_footer2.js"></script>
<script>
footerFunction("1_basic_web_page_with_links", "1bwpwl.html");
</script>
Run Code Online (Sandbox Code Playgroud)