PDA

View Full Version : How to send mouse click to window


jblock
08-28-2002, 02:39 PM
Can anyone show me how to send a mouse click to the browser window with javascript, in effect, the same as just moving the mouse over a blank area of the page and clicking?
I have a feeling this is very easy but I'm a novice and can't figure it out. Thanks.

beetle
08-28-2002, 03:02 PM
document.body.click();

jkd
08-28-2002, 04:19 PM
The proper DOM2 way to create and fire the most generic click event:

var evt = document.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, document.body);
document.body.dispatchEvent(evt);

Looks a lot more complicated, but if you read the specs and see what each argument does, you'll find it is very powerful.

beetle
08-28-2002, 04:26 PM
Thanks for that info jkd.

But doesn't only Gecko support DOM2? And does the generic click() method work w/Gecko?

brothercake
08-28-2002, 04:32 PM
document.onclick = callMyFunction


will work on every browser

jblock
08-28-2002, 05:15 PM
Thanks for everyone's help. Neither suggestion has worked for me. All I'm trying to do is send a mouse click to the browser window after I exit a little popup window function. Here's the code with the errors I got with each suggestion. BTW, this has to work across all browsers, not just newer ones.

<script type="text/javascript" language="JavaScript1.2">

function PopupWindow(popFile, popWidth, popHeight, scrolling) {

remote=window.open(popFile,"","width=" + popWidth + ",height=" + popHeight + "," + scrolling + ",left=" + page.x50 + ",top=200");

// beetle's suggestion. Result: NS4 Error document.body has no properties
// document.body.click();


// jkd's suggestion: Result: IE6 Error Object doesn't support this property or method
// NS4 Error document.createEvent is not a function
// var evt = document.createEvent('MouseEvents');
// evt.initMouseEvent('click', true, true, document.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, document.body);
// document.body.dispatchEvent(evt);

}

</script>

beetle
08-28-2002, 06:27 PM
Originally posted by brothercake
document.onclick = callMyFunction


will work on every browser Then how come this only alerts at pageload and doesn't fire with any click. IE6<html>
<head>
<title>Test</title>
<script>
document.onclick = fnAlert();
function fnAlert() {
alert('click');
}
</script>
</head>

<body>
text
</body>
</html>

jblock
08-28-2002, 07:21 PM
Turns out Beetle's suggestion

document.body.click();

works in IE6 and I presume other DOM2 browsers. It does not work in Netscape 4.x. Any suggestions for Netscape? Thanks.

beetle
08-28-2002, 07:34 PM
Ya, I do (http://www.codingforums.com/showthread.php?s=&postid=23125#post23125) :D

scroots
08-28-2002, 08:14 PM
beetle can you expand on:
document.body.click();
how would you use that to make a simple alert, would it be:

<script>
document.body.click();
function click()
alert('jhjjhjhjjh');
}

</script>


scroots

jkd
08-28-2002, 08:26 PM
document.body.fireEvent('onclick')

should work in IE.

beetle
08-28-2002, 08:42 PM
scroots - no

document.body.click() just simulates as mouse click within the BODY. So, to get it to do anything, you'd have to put your code in the BODY's onClick event. However, it will obviously also fire every time there's a real click on the page too, so this probably won't get you the results you want.

What DO you need this click for?

scroots
08-28-2002, 08:48 PM
i have a floating image in a div and when the user clicks on the page anywhere i want it to hide the div. to close the div the function is called "closediv"

scroots

brothercake
08-28-2002, 08:54 PM
Wow .. what a convoluted thread ...

beetle - when you call a function like that, you don't use the brackets, so

document.onclick = fnAlert;

will work but

document.onclick = fnAlert();

won't work

jblock - using jkd's gecko method and beetle's IE method (which will also work in opera ... i think) you've got most of them covered. Then, for netscape 4, you should be able to do this:

document.captureEvents(Event.CLICK);
document.onclick = someFunction;

document.click()


scroots - click() is a built in function - going object.click() fires the onclick event for that object.

scroots
08-28-2002, 08:56 PM
thanks brothercake, helps a little but i will have to think harder for the solution to my problem.

scroots

brothercake
08-28-2002, 08:57 PM
you should start another thread really

jblock
08-28-2002, 09:12 PM
brothercake,

Thanks but NS4 code not working for me. Try this little test page. The IE code works, NS4 code doesn't. I'm not sure what to do with those other lines you provided. My goal is to dispatch a mouse click, not capture the event. document.body.click() dispatches the event to IE, document.click() does not appear to do the same in NS4.

<html>
<head>
<title>Test</title>
</head>


<!-- This works in IE
<body onClick="alert('click');" onKeyPress="document.body.click();">
-->


<!-- NS4 -->
<body onClick="alert('click');" onKeyPress="document.click();">

text



</body>
</html>

beetle
08-28-2002, 09:16 PM
Originally posted by brothercake
document.onclick = fnAlert;

will work but

document.onclick = fnAlert();

won't workI see. What if fnAlert has parameters/arguments?