View Full Version : click on image on page load
lord22
08-02-2008, 09:10 PM
Is it possible to click on image on page load or right after it's loading?
thanks!
Jesdisciple
08-02-2008, 09:17 PM
What?? JavaScript doesn't control the mouse... Do you mean give focus to an image? I don't know that images take focus...?
lord22
08-02-2008, 09:22 PM
I don't really care about the mouse
I have an image that:
onmousedown="myCommand('wan', event, 'gold')"
I want it t happen also on page load
Jesdisciple
08-02-2008, 09:34 PM
document.getElementById('your_image').onmousedown();
By the way, it's considered "best practice" to assign event-handlers in the JS (functionality) rather than the HTML (layout).
EDIT: But the event will be null because you can't make your own event.
lord22
08-02-2008, 09:44 PM
Thank you, but I guess it is not working on page load because the image wasn't upload yet.
Is there a way to do it after the page load?
by the way this is the code I used:
window.onload=function(){
document.getElementById('righton').onmousedown();
}
Jesdisciple
08-02-2008, 09:51 PM
Odd... I think that should be after the page has loaded. Images have an 'onload' handler too, but of course the JS can't get to it before the image has loaded. So try this:
<img id="righton" onmousedown="foo('bar');" onload="this.onmousedown();">
lord22
08-02-2008, 10:00 PM
Thank you again!
I just don't get why steel it is not working on my specific page.
I'll try to edit it a bit..
Jesdisciple
08-02-2008, 10:11 PM
What's your page look like? If it's online, can I have the URL?
I just tested it and now understand why the image wasn't loaded when the page was: The browser has to wait on them because they take so long and having the text ASAP is more important than having everything at the same time.
mburt
08-03-2008, 03:50 AM
<img id="righton" onmousedown="foo('bar');" onload="this.onmousedown();">
What?
Just use the onload even handler...
<script type="text/javascript">
window.onload = function() {
foo('bar');
};
</script>
Jesdisciple
08-03-2008, 04:30 AM
The way I understand it, the function that should be called won't work until the image is loaded. But it's a good idea to verify such an assumption.
lord22
08-03-2008, 05:05 AM
The way I understand it, the function that should be called won't work until the image is loaded. But it's a good idea to verify such an assumption.
You are right..
mburt
08-03-2008, 09:05 PM
I didn't realize the onload event handler was even possible with the img tag. Sorry.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.