Maneesh mehta
Maneesh mehta

Reputation: 43

java script alert a message when user click on window

I am working on a php site.I want to use java script code for alert message when user click on anywhere in window.i am using window.onclick=function name()

function function name()
{
alert('message');

}

but it is not working.

Upvotes: 1

Views: 209

Answers (5)

Rakesh
Rakesh

Reputation: 353

use document.onclick = yourfunction() {}

Upvotes: 0

Nikhil D
Nikhil D

Reputation: 2509

document.onclick = clickMe;    
function clickMe()
    {
        alert("clickMe");
    }

window.onclick did not work when tested in IE8. So apparently the bottom line is that document.onclick is the preferred choice.

Upvotes: 1

Asciiom
Asciiom

Reputation: 9975

You're assigning the click handler wrong, try

window.onclick = name;

Upvotes: 2

Ankur
Ankur

Reputation: 12774

It has to be

function name()
{
alert('message');

}

and then you need to use window.onclick='name()';

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15861

function function name() this line making issue. you declared function as twice. for this javascript is breaking.

use like this

function name()
{
 alert('sds');
}

then assign function to the click handler like this.

window.onclick = name;

Upvotes: 1

Related Questions