Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Javascript Form / Button in Applescript

    I am writing a script to add an item to card in Applescript. Usually some variant of this works with most shopping sites:

    do JavaScript "document.getElementById('addToCart').onclick()" in document 1

    However, I am lost here. How would I "click" this one?

    a href="javascript:fnAddItemToCartWithAccessories('1218308267319','2074657')" onclick="s_objectID="javascript:fnAddItemToCartWithAccessories('1218308267319','2074657')_1";return this.s_oc?this.s_oc(e):true">
    <img id="addtocart" border="0" alt="Add to Cart" src="http://images.bestbuy.com/BestBuy_US/en_US/images/global/buttons/btn_addtocart_pdp.gif">

    http://www.bestbuy.com/site/LG+-+21....&skuId=2074657

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    This doesn't make sense on a couple of levels:
    Code:
    onclick="s_objectID="javascript:fnAddItemToCartWithAccessories('1218308267319','2074657')_1";return this.s_oc?this.s_oc(e):true"
    (1) If you use javascript: in an event handler, it must be the very first thing.
    That is, onclick="javascript:..."
    (2) You have " marks *INSIDE* of a "..." string.

    HTML will hiccup on that and it will never get to javascript.

    HTML sees your code there like this:
    Code:
    onclick="s_objectID="
    
    javascript:fnAddItemToCartWithAccessories('1218308267319','2074657')_1
    
    ";return this.s_oc?this.s_oc(e):true"
    As three distinct separate elements.

    It has no idea what to do with the 2nd or 3rd of those.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    The code was taken from the Best Buy website. I have included the link in the first post and here is a screenshot from firebug.
    Javascript Form / Button in Applescript-jed78.png

    I am trying to "click" the add to cart button using a do Javascript command inside an Applescript.

    Examples of Applescript/Javascript for other sites are:

    do JavaScript "WALMART.cart.checkout();" in document 1

    do JavaScript "document.getElementById('addToCart').onclick()" in document 1

    do JavaScript "document.forms[1].submit();" in document 1

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Bizarre.

    When I view it with FF and Firebug, I only see
    Code:
    <a href="javascript:fnAddItemToCartWithAccessories('1218308267319','2074657')">
    Nothing else in the <a> tag, at all.

    Well, in any case, I *THINK* you could do it by looking for the id of the <img> there and then going "up" in the DOM to the <a>.

    Something like
    Code:
    document.getElementById("addToCart").parent.onclick()
    maybe?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That didn't work either! Thanks for your time though.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    You know, if you had just played around with a bit, you would have gotten it.

    Code:
    eval( document.getElementById("addtocart").parentNode.href.substring(11) );
    Note that the id of the image is "addtocart" *ALL LOWER CASE*.

    And rather than attempting to make the CLICK on the <a> work for all browsers, since the href is actually a javascript call, I just stripped off the "javascript:" and then called the function via eval. Presto.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    adayzdone (12-04-2011)

  • #7
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That worked perfectly. Thank you.

    Code:
    tell application "Safari"
    	activate
    	do JavaScript "eval(document.getElementById('addtocart').parentNode.href.substring(11))" in document 1
    end tell
    Can you point me in the right direction of the best JS for beginners resource?

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,561
    Thanks
    80
    Thanked 4,496 Times in 4,460 Posts
    Look in the top of the threads list in this forum.

    Look at the 2nd and 3rd "Sticky" threads.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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