javascript question!

Discussion in 'Code Snippets and Tutorials' started by Fexxel, Apr 21, 2009.

  1. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    I just found out that there was a do JavaScript command in Apple Script, so....
    How do I get JavaScript to visit a URL in the active web browser window?
    The following opens in a new window, but I want it to open in the current window and then close it after 2 seconds (and pause for one):
    Code (Text):
    1. tell application "Safari"
    2.     activate
    3.     do JavaScript "window.open('http://www.aol.com')" in document 1
    4.     delay 2
    5.     tell application "System Events"
    6.         tell process "Safari"
    7.                         delay 2
    8.             keystroke "w" using {command down}
    9.                        delay 1
    10.         end tell
    11.     end tell
    12. end tell
    13.  
     
  2. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Code (Text):
    1. do JavaScript "document.location.href = 'http://www.yourpage.com/';" in document 1
     
  3. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Thanks Ricky! I'd +Rep but I have to +rep like 4 other people before +repping you again.
    ---
    Could I make javascript go to multiple pages or would I have to do something like this:
    Code (Text):
    1. delay x
    2. do JavaScript "document.location.href = 'http://www.page1.com/';" in document 1
    3. delay x
    4. do JavaScript "document.location.href = 'http://www.page2.com/';" in document 1
    5. delay x
    ?
    :)
    Thanks Ricky!!!!!
     
  4. Will

    Will Level IV

    Joined:
    Mar 1, 2009
    Messages:
    1,067
    Likes Received:
    53

    If you mean opening multiple tabs or browsers, that isn't how it's done..

    If you mean visiting one page, waiting, then visiting another and so on: I think that's how it's done.. but I'm not fantastic with javascript.