0

I need ur help i know this is a silly question but am not able to solve it ...sorrry :(

I have one javascript file welcome.js in that i have

    function alert()
    {
    return 10;
    } 

I have another html file welcome.html in which am invoking the welcome.js file

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

I keep on getting this error in mozilla console

Timestamp: 5/28/2013 9:56:47 PM Error: NS_ERROR_XPC_NOT_ENOUGH_ARGS: Not enough arguments [nsIDOMWindow.alert]

2
  • 1
    @acdcjunior It can, jsfiddle.net/G5xKB
    – Niklas
    Commented May 28, 2013 at 16:42
  • 1
    @Niklas good catch! (deleted my comment as it was misleading)
    – acdcjunior
    Commented May 28, 2013 at 16:46

3 Answers 3

5

Your javascript (welcome.js) isn't getting loaded do to the syntax error in your script tag. You are missing the " quote after text/javascript.

The alert(); is calling window.alert() which requires an argument to be passed in Firefox (doesn't for example in Chrome). As your own welcome.js never gets loaded (due to the syntax error), it never ends up rewriting the global alert() function.

0
3

alert function name shouldn't be overriden, because in that case if you use

alert(something)

you will call to your new function and not to the dialog function

You have 2 options:

  • put another name to your function alert
  • when you need to call to dialog function use window.alert(something) to differentiate
2
  • It isn't reserved, it just never is getting overwritten.
    – Niklas
    Commented May 28, 2013 at 16:40
  • @Niklas this is a best replication of the problem, and doesnt work: jsfiddle.net/G5xKB/1 Commented May 28, 2013 at 16:51
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">

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.