JS文件已加载但未执行

JS文件已加载但未执行

本文介绍了JS文件已加载但未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个有趣的问题,很可能是由于我无法察觉的愚蠢错误。
使用Markdown生成HTML文件。
我在html中包含了一个JavaScript文件。它是可见的,它在浏览器中加载(在Mozilla Firefox中尝试)。



请参阅下面的证明。





但不知何故,它没有被执行。



以下是我的html

 <!DOCTYPE html> 
< html lang =en>
< head>
< meta charset =UTF-16/>
< meta name =viewportcontent =width = device-width,initial-scale = 1,maximum-scale = 1/>
< meta name =generatorcontent =Bootply-JBake-Custom/>
< meta http-equiv =content-typecontent =text / html; charset = UTF-16/>

< title>标题< / title>
< link href =/ css / bootstrap.min.css =stylesheet>
< link href =/ css / asciidoctor.css =stylesheet>
< link href =/ css / base.css =stylesheet>
< link href =/ css / prettify.css =stylesheet>
< link href =/ css / styles.css =stylesheet>
< link href =/ css / animate.css =stylesheet>
< link rel =快捷图标href =favicon.ico>
< script src =/ js / jquery-1.11.1.min.js>< / script>
< script src =/ js / bootstrap.min.js>< / script>
< script src =/ js / prettify.js>< / script>
< / head>
< body onload =prettyPrint()>
< div id =wrap>

< div class =navbar navbar-default navbar-fixed-top>
< div class =container>
< div class =navbar-header>
< a href =/ index.htmlclass =navbar-brand> LOGO< / a>
< button class =navbar-toggletype =buttondata-toggle =collapsedata-target =#navbar-main>
< span class =icon-bar>< / span>
< span class =icon-bar>< / span>
< span class =icon-bar>< / span>
< / button>
< / div>
< div class =navbar-collapse collapseid =navbar-main>
< ul class =nav navbar-nav>
< li>< a href =/ projects / projects.html>项目< / a>< / li>
< li>< a href =/ about.html>关于< / a>< / li>
< li>< a href =/ categories.html>类别< / a>< / li>
< / ul>
< / div>
< / div>
< / div>
< div class =container>
< div class =page-headerid =banner>
< div class =row>
< div class =col-lg-8 col-md-7 col-sm-6>
< h1>项目< / h1>
< / div>
< / div>
< / div>
< script type =text / javascriptsrc =projects.js>< / script>
< p>以下是项目清单< / p>

< / div>
< / div>
< div id =push>< / div>
< div id =footer>
< div class =container>

< / div>
< / div>

< / body>
< / html>

projects.js

  $(document).ready(function(){
console.log(ready)
alert(hi);
});

console.log(loaded)


解决方案 div>

< script type =text / javascriptsrc =projects.js>< / script>



删除 type =text / javascript 中的结果:

< script type =text / javascriptsrc =projects.js>< / script>

这应该允许浏览器正确解析HTML并加载脚本。



编辑:或者删除类型属性完全来自< script> 标签,如下面的注释所示。


I am facing a funny issue, most probably due to a silly mistake that I am not able to detect.The HTML file is generated using Markdown.I have included a JavaScript file in html. It is visible that it is loaded in browser (tried on mozilla firefox).

See the proof below.

I tested if JS is enabled or not from https://www.whatismybrowser.com/detect/is-javascript-enabled

But somehow, it is not being executed.

Below is my html

<!DOCTYPE html>
<html lang="en">
  <head>
        <meta charset="UTF-16"/>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="generator" content="Bootply-JBake-Custom"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-16"/>

<title>Title</title>
    <link href="/css/bootstrap.min.css" rel="stylesheet">
    <link href="/css/asciidoctor.css" rel="stylesheet">
    <link href="/css/base.css" rel="stylesheet">
    <link href="/css/prettify.css" rel="stylesheet">
    <link href="/css/styles.css" rel="stylesheet">
    <link href="/css/animate.css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico">
<script src="/js/jquery-1.11.1.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
<script src="/js/prettify.js"></script>
  </head>
  <body onload="prettyPrint()">
    <div id="wrap">

<div class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <a href="/index.html" class="navbar-brand">LOGO</a>
          <button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#navbar-main">
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
        </div>
        <div class="navbar-collapse collapse" id="navbar-main">
          <ul class="nav navbar-nav">
                          <li><a href="/projects/projects.html">Projects</a></li>
              <li><a href="/about.html">About</a></li>
              <li><a href="/categories.html">Categories</a></li>
          </ul>
        </div>
      </div>
    </div>
    <div class="container">
        <div class="page-header" id="banner">
            <div class="row">
                <div class="col-lg-8 col-md-7 col-sm-6">
                    <h1>Projects</h1>
                </div>
            </div>
        </div>
         <!-- This script is loading but not executing -->
         <script type="text /javascript" src="projects.js"></script>
<p>Here is the list of projects</p>

    </div>
        </div>
        <div id="push"></div>
    <div id="footer">
      <div class="container">

      </div>
    </div>

  </body>
</html>

projects.js

$(document).ready(function(){
    console.log("ready")
alert("hi");
});

console.log("loaded")
解决方案

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

Remove the space in type="text /javascript with the result:

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

This should allow the browser to parse the HTML correctly and load the script.

EDIT: Or remove the type attribute from the <script> tag entirely, as suggested by the comment below.

这篇关于JS文件已加载但未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:49