JS文件加载在一个页面上但不加载另一个

时间:2017-10-23 05:05:41

标签: javascript jquery html

我的.js文件加载一页但不加载另一页。我已经点击了它无法正常工作的页面上的JS文件,它似乎正在加载。

这不起作用

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=yes">
  <title>Homepage</title>
  <link href="{% static "css/app_search.css" %}" rel="stylesheet"></link>
  <!-- CSS reference: https://bootswatch.com/yeti/ -->
</head>

<body>

<!-- The header -->
  <div class="container">
    <div class="page-header" id="banner">
      <div class="row">
        <div class="col-lg-8 col-md-7 col-sm-6">
          <h1>Home page/h1>
        </div>
        <a href="{% url 'app_new:new' %}">Submit a Collection Request</a>
      </div>
    </div>
  </div>
  <br>

<!-- The form -->
BLAH BLAH (TOOK OUT CODE TO SHORTEN)

<gcse:searchbox-only></gcse:searchbox-only>
</div>
<script src="{% static "js/scripts.js" %}"></script>
</body>

</html>

同时,这有效:

<!DOCTYPE html>
{% load staticfiles %}
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=yes">
  <title>Seek</title>
  <link href="{% static "css/app_search.css" %}" rel="stylesheet">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <!-- CSS reference: https://bootswatch.com/yeti/ -->
</head>

<body>

!~form BLAH BLAH TOOK OUT CODE TO SHORTEN

<script src="{% static "js/scripts.js" %}"></script>
</body>

</html>

这是JS文件......

$(document).ready(function(){
    $(":submit").click(function(){
        alert("Thank you for submitting a website request!");
    });

    !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="4.0.0";
    analytics.load("key");
    analytics.page();
    }}();

    (function() {
      var cx = 'took out';
      var gcse = document.createElement('script');
      gcse.type = 'text/javascript';
      gcse.async = true;
      gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
      var s = document.getElementsByTagName('script')[0];
      s.parentNode.insertBefore(gcse, s);
    })();

});

这很奇怪,因为如果JS错了,那么两个页面都不应该正常工作。我没有收到任何文件未找到错误等。

1 个答案:

答案 0 :(得分:1)

您正在使用jQuery而不导入它。导入jQuery脚本,如工作页面所示。​​

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
相关问题