Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: click on image on page load

  1. #1
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Question click on image on page load

    Is it possible to click on image on page load or right after it's loading?

    thanks!

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    What?? JavaScript doesn't control the mouse... Do you mean give focus to an image? I don't know that images take focus...?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

    I don't really care about the mouse
    I have an image that:
    PHP Code:
    onmousedown="myCommand('wan', event, 'gold')" 
    I want it t happen also on page load

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Code:
    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #5
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

    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:

    PHP Code:
    window.onload=function(){
    document.getElementById('righton').onmousedown();


  6. #6
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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:
    Code:
    <img id="righton" onmousedown="foo('bar');" onload="this.onmousedown();">
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  7. #7
    Join Date
    Jul 2008
    Posts
    81
    Thanks
    38
    Thanked 2 Times in 2 Posts

    Default

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

  8. #8
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    <img id="righton" onmousedown="foo('bar');" onload="this.onmousedown();">
    What?

    Just use the onload even handler...

    Code:
    <script type="text/javascript">
    window.onload = function() {
      foo('bar');
    };
    </script>
    - Mike

  10. #10
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •