IE 中 Javascript 文件的外部引用

发布于 2024-08-09 15:26:31 字数 757 浏览 3 评论 0原文

我正在为一位老师开发一个网站, 由于她以后可能想添加更多内容,所以我决定将每个页面上出现的链接都用Javascript编写,以便可以快速更改所有页面。

HTML 代码具有以下内容供外部参考:

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

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>)

这在 firefox 中完美加载,但由 javascript 编写的页面部分在 IE 中无法加载。是代码的编写方式还是引用阻止了 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.

The HTML code has this for the external reference:

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

The Javascript looks 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>)

This loads perfectly in firefox, but the part of the page written in by javascript does not load in IE. is it how the code is written or the reference that is preventing IE from loading it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌︼了一个春 2024-08-16 15:26:31

您需要像这样在它周围加上引号:

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>');

但是,如果它只是在 HTML 文件中输出,那么它会干净得多。我想如果 Javascript 来自老师控制下的外部域,这就是这样做的原因之一。

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. 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.

z祗昰~ 2024-08-16 15:26:31

您的引号未正确转义。尝试这样:

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>");

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>");
把回忆走一遍 2024-08-16 15:26:31

将链接放在单独的 HTML 文件中并将其包含在所有页面上。所有主要的网络服务器都支持这一点。例如:

  • 如果您有 PHP,则可能是
  • 如果您有 Apache,则可能是

Put the links in a separate HTML file and include it on all pages. All major web servers support this. For example:

  • If you have PHP, it'd could be <?php include('links.html'); ?>
  • If you have Apache, it could be <!--#include virtual="/links.html" -->
你对谁都笑 2024-08-16 15:26:31

我建议为您维护的链接创建数组,然后可以将其放入类似的函数中:

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>');

这在服务器端代码(如 PHP)中比 Javascript 更有意义,真的,并且方法大致相同。这种风格是朝着创建一个从数据库读取项目然后使用类似功能打印出来的网站迈出的良好一步。

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. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文