Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-02-2011, 09:28 PM   PM User | #1
adayzdone
New to the CF scene

 
Join Date: Dec 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
adayzdone is an unknown quantity at this point
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
adayzdone is offline   Reply With Quote
Old 12-02-2011, 11:34 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 12-03-2011, 02:14 AM   PM User | #3
adayzdone
New to the CF scene

 
Join Date: Dec 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
adayzdone is an unknown quantity at this point
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.
Click image for larger version

Name:	jed78.png
Views:	20
Size:	38.8 KB
ID:	10566

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
adayzdone is offline   Reply With Quote
Old 12-03-2011, 02:27 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Old 12-03-2011, 05:59 AM   PM User | #5
adayzdone
New to the CF scene

 
Join Date: Dec 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
adayzdone is an unknown quantity at this point
That didn't work either! Thanks for your time though.
adayzdone is offline   Reply With Quote
Old 12-03-2011, 06:16 AM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
adayzdone (12-04-2011)
Old 12-04-2011, 06:54 PM   PM User | #7
adayzdone
New to the CF scene

 
Join Date: Dec 2011
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
adayzdone is an unknown quantity at this point
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?
adayzdone is offline   Reply With Quote
Old 12-04-2011, 09:22 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,555
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Tags
applescript, click, submit

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:41 AM.


Advertisement
Log in to turn off these ads.