Dave C
Dave C

Reputation: 262

Cross Browser Automatically Click Link On Page Load with Javascript

I have a simple html page with an iframe image link. After the page loads I want the link to be clicked automatically. I need the link to be clicked instead of doing a meta refresh so thats out of the question. I searched around and It seems like FireFox does not support any methods to do so...There has to be a cross browser solution. Here is what I have so far but it wont work...why?

 <script type="text/javascript">
window.onload=functionName;
$(document).ready(function(){
  $('#clicked').trigger('click')
});
</script>

<div id="clicked">
      <iframe src="http://mysource.com" width="40" height="10" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>
  </div>

Upvotes: 0

Views: 5475

Answers (2)

Anthony Manning-Franklin
Anthony Manning-Franklin

Reputation: 5008

If the iframe is on your domain then you can access its contents via jquery selectors.

Otherwise you're out of luck - blocking cross domain iframe interactions is a feature of same-origin policy that exists to protect users.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Scripting

Upvotes: 1

Nagarajan A
Nagarajan A

Reputation: 89

Add # before the id value

$('clicked').trigger('click');

to

$('#clicked').trigger('click')

Upvotes: 2

Related Questions