0

我需要你的帮助我知道这是一个愚蠢的问题但我无法解决它......对不起:(

我有一个javascript文件welcome.js,我有

    function alert()
    {
    return 10;
    } 

我有另一个 html 文件welcome.html 在其中调用welcome.js 文件

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

    <script type="text/javascript src="welcome.js"></script>
    <script>
    function myFunction()
    {
     var x = alert();
    return x;
    }
    </script>
    </head>

    <body>
     <input type="button" onclick="myFunction();" value="Show alert box">

我不断在 Mozilla 控制台中收到此错误

时间戳:5/28/2013 9:56:47 PM 错误:NS_ERROR_XPC_NOT_ENOUGH_ARGS:没有足够的参数 [nsIDOMWindow.alert]

4

3 回答 3

5

您的 javascript (welcome.js) 未加载是因为您的脚本标记中的语法错误。您缺少."之后的报价text/javascript

alert();is 调用需要在window.alert()Firefox 中传递一个参数(例如在 Chrome 中不需要)。由于您自己的welcome.js 永远不会被加载(由于语法错误),它永远不会重写全局alert() 函数。

于 2013-05-28T16:40:11.760 回答
3

不应覆盖警报函数名称,因为在这种情况下,如果您使用

alert(something)

你将调用你的新函数而不是对话框函数

您有 2 个选项:

  • 为您的功能警报添加另一个名称
  • 当您需要调用对话框功能时使用window.alert(something)来区分
于 2013-05-28T16:38:40.960 回答
0

You can not override window.alert() function, try the below:

 function test()
    {
    return 10;
    }

Then in you html use test()

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<script type="text/javascript src="welcome.js"></script>
<script>
function myFunction()
{
 var x = test();
return x;
}
</script>
</head>

<body>
 <input type="button" onclick="myFunction();" value="Show alert box">
于 2013-05-28T16:44:06.677 回答