简体   繁体   English

script.js文件未链接到具有脚本标签src属性的html文件

[英]script.js file not linking to to html files with script tag src attribute

在此处输入图片说明 I am new to JavaScript. 我是JavaScript新手。 When I am trying the following code my js files are not getting linked and the JavaScript code from the html page is not executed : Both html and JavaScript files are at the same path. 当我尝试以下代码时,我的js文件未链接,并且html页面中的JavaScript代码未执行:html和JavaScript文件位于同一路径。

MY html: 我的HTML:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;>
<title>JS Code</title>
</head>
<body>
    HTML
    <script type="text/javascript" src="script.js">
    debugger;
    document.write("HI i am from HTML with js code")
    nowCall1();
    function nowCall1(){
        document.write("-Js.")
        //peakOil(changeCivilisationCallback); 
    }
    </script>
</body>
</html>

script.js: script.js:

function peakOil(callback) {
    document.write("HI i am from Js");
    callback();  // the parentheses mean the function is executed!

}

function changeCivilisationCallback(){
     document.write("HI i am from changeCivilisationCallback");
}

function nowCall(){
    document.write("Js.")
    //peakOil(changeCivilisationCallback); 
}

But when I try to remove scr attribute put all code in html script is working. 但是,当我尝试删除scr属性时,将html脚本中的所有代码都正常工作。 Why when I try to put code in js file, it is not executed. 为什么当我尝试将代码放入js文件时,它没有执行。 I am facing problem in debugging in google chrome browser. 我在Google Chrome浏览器中调试时遇到问题。

Please help me understand javas cript, thanks in advance. 请帮助我理解Java cript,在此先感谢。

if the src attribute is present on the script tag the content of the scrip tag will be ignored, so you need to add your external script on a seperate script tag. 如果script标签上存在src属性,则scrip标签的内容将被忽略,因此您需要在单独的script标签上添加外部脚本。

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
//code here
</script>

You should either link to an external script file with the script tag, or write the code inside the tag, you can't do both (but of course you can have more than one script tag). 您应该使用script标记链接到一个外部脚本文件,或者在标记中编写代码,但不能两者都做(但是当然可以有多个script标记)。

You can find a note on this on w3schools : 您可以在w3schools上找到关于此的注释:

If the "src" attribute is present, the <script> element must be empty. 如果存在“ src”属性,则<script>元素必须为空。

This is the corrected JavaScript. 这是更正的JavaScript。

JS code from HTML: HTML的JS代码:

document.write("HI i am from HTML with js code");
nowCall1();

function nowCall1() {
    document.write("-Js.");
        //peakOil(changeCivilisationCallback); 
}

script.js: script.js:

function peakOil() {
    document.write("HI i am from Js");
}
peakOil();

function changeCivilisationCallback() {
    document.write("HI i am from changeCivilisationCallback");
}

function nowCall() {;
    document.write("Js.")
        //peakOil(changeCivilisationCallback); 
}

In the HTML you should add the scripts this way: 在HTML中,您应该通过以下方式添加脚本:

<script type="text/javascript">
    //the rest of the code
</script>

I have missed a " after content= "text/html on the meta tag 我错过了meta标签上的" after content = "text/html之后

With this correction that it work fine, 经过这项修正,它可以正常工作,

<html>
<head>
<meta http-equiv="Content-Type" content="text/html">
<title>JS Code</title>
</head>
<body>
    HTML
    <script type="text/javascript" src="script.js"></script>
    <script type="text/javascript">
    debugger;
    document.write("HI i am from HTML with js code");
    alert("test1"); 
    document.writeln("\ after alert");
    nowCall();
    alert("test2"); 
    </script>
    </body>
</html>

Keja , Thanks it worked, with your input. Keja ,非常感谢您的投入。

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

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