【问题标题】:Browser doesn't recognize inclusion of external javascript/jquery file浏览器无法识别包含外部 javascript/jquery 文件
【发布时间】:2014-04-04 20:35:59
【问题描述】:

在我使用 Spring MVC 框架开发的动态 Web 项目 (Eclipse) 中,我有下面的 JSP 页面,放在文件夹 WEB-INF/jsp/ 中:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <title>HorarioLivre</title>

  <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
  <script src="js/index.js"></script>

  <link rel="stylesheet" href="css/style-main.css">
  <link rel="stylesheet" href="css/style-popup.css">
</head>
<body>
  <header>
    <div class="container">
      <h1><a href="#">HorarioLivre</a></h1>
      <nav>
        <ul>
          <li><a href="listagem_evento.html" class="icon evento">Eventos</a></li>
          <li><a href="cadastra_horario.html" class="icon horario">Cadastrar Horarios</a></li>
          <li><a href="listagem_horario.html" class="icon horario">Listar Horarios</a></li>
          <li><a href="listagem_usuario.html" class="icon usuario">Usuarios</a></li>
          <li><a href="#">${usuario.nome}</a>
            <ul>
                <li><a href="usuario_perfil.html" class="icon perfil">Perfil</a></li>
                <li><a href="usuario_config.html" class="icon settings">Configura&ccedil;&otilde;es</a></li>
                <li><a href="#">Logout</a></li>
            </ul>
          </li>
        </ul>
      </nav>
    </div>
  </header>
  <div id="results">
        <a href="#" id="close">Fechar</a>
        <div id="content"></div> 
  </div>  
</body>
</html>

我的问题是应用程序显然没有读取放置在文件夹 WebContent/js 中的 js/index.js 文件(放置在 WebContent/css 中的 css 文件可以正常读取)。当我将一些 javascript/jquery 代码直接放在 JSP 页面中时(如问题 Browser doesn't recognize javascript / jquery code 中显示的代码),它们执行时没有任何问题。

有人能找到这个页面的问题吗?

更新 1

$(document).ready(function(){
   setupPopup();
});

function setupPopup() {
   $('a').click(function() {
       $('#content').load($(this).attr('href'));
      $('#container').append('<div id="cover">');
      $('#results').fadeIn(500);
      popupPosition();
   });

   $('#close').click(function() {
      $('#results').fadeOut(100);
      $('#cover').remove();
   });

   $(window).bind('resize', popupPosition);
}

function popupPosition() {
   if(!$("#results").is(':visible')){ return; }

   $("#results").css({
      left: ($(window).width() - $('#results').width()) / 2,
      top: ($(window).width() - $('#results').width()) / 7,
      position:'absolute'
   });

   $('#results').draggable();
}

最终更新

最后,我选择不使用jquery,并用这个替换标题中的代码:

<script>
  function handleClick(url){
      document.getElementById("results").style.display = 'block';
      document.getElementById("content").innerHTML='<object type="text/html" data="'+url+'" ></object>';
      }
  function cleanDiv() {
      document.getElementById("results").style.display = 'none';
      document.getElementById("content").innerHTML=' ';
  }
  </script>

每个链接都有这种格式:

<a href="#" onclick="handleClick('listagem_evento.html')" class="icon evento">Eventos</a>

我的弹出窗口的 html 代码得到这个表单:

<section class="about" id="results" style="left: 183px; top: 111px;" onMouseDown="dragStart(event, 'results');">
    <div align="right"><a href="#" class="classname" onclick="cleanDiv()">X</a></div>
    <div id="content" align="center"></div>
  </section>

与方style="left: 183px; top: 111px;" onMouseDown="dragStart(event, 'results');" 负责在屏幕上移动弹出窗口(见问题how to drag and drop a <div> across the page)

【问题讨论】:

  • 您有一个js/index.js... 的相对文件路径,因此它将相对于包含 js 包含的页面的位置进行加载。如果您使用 Fiddler2 或其他工具查看流量,您将收到该文件的 404 错误。
  • 如果您使用的是 Chrome 或 Firefox(或 IE 9+),浏览器中的开发工具有一个网络面板,它会显示浏览器加载文件时出现的错误。铬:developers.google.com/chrome-developer-tools/docs/… 火狐:developer.mozilla.org/en-US/docs/Tools/Network_Monitor IE:blogs.msdn.com/b/ie/archive/2010/04/22/…
  • 我使用 Firefox 的网络监视器查看文件加载过程中可能出现的任何错误,并且我可以验证所有文件都正确加载,包括 index.js。通过这种方式,我无法弄清楚为什么代码不起作用(在问题中添加了 index.js 的内容)。
  • 您能否将屏幕截图链接到 Firefox 的网络监视器结果?
  • 这是截图 -> imgur.com/77VDQvP

标签: javascript jquery html jsp


【解决方案1】:

我不完全确定,但我想我以前见过这样做: 从您的 js 源中删除 http: 和 https:。所以它应该是这样的:

<script src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>

这可能行不通,这只是我的一个想法。

【讨论】:

  • 作为您的参考,这样做的原因是因为它允许文件回退到页面的协议。例如,如果页面加载了https://,那么js文件将加载https://。如果文件是用http:// 硬编码的,那么即使页面是通过https:// 协议获取的,也会通过http:// 协议获取文件。 Here 是一个简洁的小参考。
  • 我遵循您对这两个包含的建议,直接放置在页面中的代码继续工作,而外部 javascript 继续不起作用。
【解决方案2】:

这不是最好的解决方案,但您可以尝试使用 jQuery.load 或类似的方式动态加载脚本

<script>document.write('<script src="js/vendor/jquery-1.10.1.min.js"><\/script>')</script>

【讨论】:

    【解决方案3】:

    改用&lt;script src="/js/index.js"&gt;&lt;/script&gt;。

    编辑:这个Absolute path & Relative Path 更详细地解释了相对路径和绝对路径。

    我建议您在浏览器中运行 JSP 后查看源代码(右键单击 - 查看页面源代码或类似内容,具体取决于您的浏览器)。您应该看到页面尝试加载的 JS 的 URL,并且应该能够相应地更正它。

    另外,可能只是 URL 中缺少您的上下文路径。在这种情况下,我会在生成 URL 时使用&lt;c:url&gt; 标签。

    【讨论】:

    • 我试过了,但还是不行。我真的认为 css 正在加载而 javascript 没有加载很奇怪,因为两者都使用相同的符号。我也不明白为什么将代码直接放在 jsp 页面中有效而包含外部 javascript 无效。
    • 虽然这可能回答了这个问题,但如果你能添加一些关于它如何以及为什么回答这个问题的解释会更好。
    猜你喜欢
    • 1970-01-01
    • 2016-12-19
    • 2010-11-22
    • 2016-03-18
    • 2015-02-11
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多