为什么地址栏中的文件名会被JavaScript函数替换

Why is the file name getting replace by a JavaScript function in the address bar?

本文关键字:JavaScript 函数 替换 文件名 地址栏 为什么      更新时间:2023-09-26

一个本地html文件,让我们说它的路径是/path/to/file.html。一开始有以下内容。

<html>
 <head>
  <link rel="shortcut icon" href="logo.ico"/>
  <LINK href="1.css" rel="stylesheet" type="text/css" />
  <script src="1.js" type="text/javascript"> </script>
 </head>
<body class = "body_background">
.
.
.

当尝试使用其完整路径在浏览器中打开时:

文件名及其扩展名(file.html)将被JavaScript函数替换。

file:///path/to/file.html

被更改为以下内容:文件名及其扩展名被位于1.js中的函数location替换。

file:///path/to/functionlocation(){

JavaScript文件的开头有这样的内容:

if(window.addEventListener) {
  window.addEventListener('load', location, false);
  }
else if (window.attachEvent) window.attachEvent('onload', location);

这发生在FireFox Safari上,而不是在Chrome到上。

Chrome上,页面将适当显示。

这个问题是给朋友的

location在一些JS实现中是一个保留字,如document.location

请参阅:http://www.javascripter.net/faq/reserved.htm