Solved

I need to simulate a click on an href anchor

Posted on 2009-05-19
12
1,272 Views
Last Modified: 2013-12-06
I have the following anchor tag that I need to simulate clicking on it on the page onload event.

Basically I need that href tag to be "executed"

[code]
<a id="ooyalaPlayer_1w0o4_fux6xvhi" title="MyGizmo :: Presents..." rel="rokbox[720 404]" href="http://vmysite.com/9xbjlqOsoApB&autoplay=1/ooyalaPlayer_1w0o>Click me to watch video</a>
[/code]
<a id="ooyalaPlayer_1w0o4_fux6xvhi" title="MyGizmo :: Presents..." rel="rokbox[720 404]" href="http://vmysite.com/9xbjlqOsoApB&autoplay=1/ooyalaPlayer_1w0o>Click me to watch video</a>

Open in new window

0
Comment
Question by:Jossif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
12 Comments
 
LVL 2

Expert Comment

by:lyhan
ID: 24427816
you can set windows.location.href = any given anchor in the onload function,



windows.location.href = "http://vmysite.com/9xbjlqOsoApB&autoplay=1/ooyalaPlayer_1w0o"

<script language="javascript" type="text/javascript">
 
 
 
windows.location.href ="http://vmysite.com/9xbjlqOsoApB&autoplay=1/ooyalaPlayer_1w0o";
 
 
</script>

Open in new window

0
 

Author Comment

by:Jossif
ID: 24427844
But that will reload the page with that URL... And as you see, the href call a function via a rel so this will not work, would it?

0
 
LVL 2

Expert Comment

by:lyhan
ID: 24427864
I see, please try

document.getElementById('ooyalaPlayer_1w0o4_fux6xvhi').click();

ooyalaPlayer_1w0o4_fux6xvhi is the id by the way
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Jossif
ID: 24427922
Nope.... It throws an error:

document.getElementById("ooyalaPlayer_1w0o4_fux6xvh").click is not a function

I think that the click event is just for buttons and not for anchors
0
 

Author Comment

by:Jossif
ID: 24427931
I meant:


document.getElementById("ooyalaPlayer_1w0o4_fux6xvhi").click is not a function
0
 

Author Comment

by:Jossif
ID: 24427971
Basically the rel in that href is calling mootools and I think that's the problem.

0
 
LVL 2

Expert Comment

by:lyhan
ID: 24428006
in your code snippet, it's malformed

<a id="ooyalaPlayer_1w0o4_fux6xvhi" title="MyGizmo :: Presents..." rel="rokbox[720 404]" href="http://vmysite.com/9xbjlqOsoApB&autoplay=1/ooyalaPlayer_1w0o<B>"</B>>Click me to watch video</a>

href="..."
0
 
LVL 2

Expert Comment

by:lyhan
ID: 24428020
it's very possible. It's not easy to find a way to simulate user clicks across different browsers
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 24429019
Yes it is


<script>
// allow firefox to handle a click - code possibly by Jason Karl Davis
if(typeof HTMLElement!='undefined'&&!HTMLElement.prototype.click)
HTMLElement.prototype.click=function(){
var evt = this.ownerDocument.createEvent('MouseEvents');
evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
this.dispatchEvent(evt);
}
</script>
 
 
document.getElementById('ooyalaPlayer_1w0o4_fux6xvhi').click();

Open in new window

0
 

Author Comment

by:Jossif
ID: 24433593
@mplungjan: I am testing your solution later today. If it works, 500 points for you!

Will this work in MSIE 7, FF and Safarr?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24433806
The code I gave is for FF, MSIE and Safari should work with the click anyway.
0
 

Author Comment

by:Jossif
ID: 24434127
500 points for you, mplugian!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question