Solved

I need to simulate a click on an href anchor

Posted on 2009-05-19
12
1,269 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This article provides a case study on how our local youth baseball league deployed a new website, including the platform selection, implementation and benefits to the league.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

856 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