简体   繁体   English

IE中Javascript文件的外部引用

[英]External Reference of a Javascript file in IE

I am working on a website for a teacher, Since there is a chance she may want to add more later, I decided to make the links that appear on every page be written in by Javascript so that all the pages could be changed quickly. 我正在为老师开发一个网站,因为她有可能想在以后添加更多,我决定让每个页面上出现的链接都是用Javascript写的,这样所有的页面都可以快速更改。

The HTML code has this for the external reference: HTML代码用于外部参考:

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

The Javascript looks like this: Javascript看起来像这样:

document.write(<div id="wrapper">
<div id="header">
<ul>
<li><a href="home.html">Home </a></li>
<li><a href="catering-home.html">Catering </a></li>
<li><a href="prostart-home.html">ProStart </a></li>
<li><a href="recipes.html">Recipes</a></li>
</ul>
</div>
</div>)

This loads perfectly in firefox, but the part of the page written in by javascript does not load in IE. 这在firefox中完全加载,但是用javascript编写的页面部分不会在IE中加载。 is it how the code is written or the reference that is preventing IE from loading it? 它是如何编写代码或阻止IE加载它的引用?

You would need quotes around it like this: 你需要这样的报价:

document.write('<div id="wrapper">'+
'<div id="header">'+
'<ul>'+
'<li><a href="home.html">Home </a></li>' +
'<li><a href="catering-home.html">Catering </a></li>' +
'<li><a href="prostart-home.html">ProStart </a></li>' +
'<li><a href="recipes.html">Recipes</a></li>' +
'</ul>' +
'</div>' +
'</div>');

However, it would be a lot cleaner if it was just output in the HTML file. 但是,如果它只是在HTML文件中输出,那将会更加清晰。 I guess if the Javascript is coming from an external domain under the teacher's control, that's one reason to do it this way. 我想如果Javascript来自外部域,由老师控制,这就是这样做的一个原因。

Your quotes are not correctly escaped. 您的报价未正确转义。 Try like this: 试试这样:

document.write("<div id=\"wrapper\"><div id=\"header\"><ul><li><a href=\"home.html\">Home </a></li><li><a href=\"catering-home.html\">Catering </a></li><li><a href=\"prostart-home.html\">ProStart </a></li><li><a href=\"recipes.html\">Recipes</a></li></ul></div></div>");

Put the links in a separate HTML file and include it on all pages. 将链接放在单独的HTML文件中,并将其包含在所有页面上。 All major web servers support this. 所有主要的Web服务器都支持此功 For example: 例如:

  • If you have PHP, it'd could be <?php include('links.html'); ?> 如果你有PHP,它可能是<?php include('links.html'); ?> <?php include('links.html'); ?>
  • If you have Apache, it could be <!--#include virtual="/links.html" --> 如果你有Apache,它可能是<!--#include virtual="/links.html" -->

I'd suggest making array for the links that you maintain, and then it can be placed into a function like: 我建议为你维护的链接制作数组,然后将其放入如下函数中:

var links=[
   ['home.html','Home'],
   ['catering-home.html','Catering'],
   ['prostart-home.html','Prostart'],
   ['recipes.html','Recipes']
  ];

document.write('<div id="wrapper">
<div id="header">;
 <ul>');
  var items=links.length;
  for(var i=0;i<items;i++){
    document.write('<li><a href="'+links[i][0]+'">'+links[i][1]+'</a></li>');
    }
document.write('</ul>
</div>
</div>');

This would make more sense in server side code like PHP than Javascript, really, and the approach would be about the same. 这在PHP等服务器端代码中比Javascript更有意义,实际上,这种方法大致相同。 This style is a good step towards making a site that reads the items from a database and then print them out with a function like so. 这种风格是一个很好的一步,使一个网站从数据库中读取项目,然后用这样的功能打印出来。

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

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