I am working with Iframes for controlling the elements of iframe by automating it from the parent html file.
I was trying to to click the link defined in iframe.html file from the test.html file using function click() on javascript while loading test.html :
Test.html file :
function handleMainOnload(){
alert("main frame loaded");
var innerFrame = document.getElementById("frame1");
alert("hi-1" + innerFrame.ownerDocument.title);
var innerDocument = frame1.document;
alert("hi" + innerDocument.title);
innerDocument.getElementById("input1").value = "Dynamically Inserted Text";
innerDocument.getElementById("a1").click();
}
<body onload="handleMainOnload();">
<iframe id="frame1" src="iframe.html"/>
iframe.html file :
<a id="a1" href="http://www.google.co.in"> Google.. click</a>
This achor id = a1 automatically get clicked onload of test.html in IE but doesn’t work on any browser apart from IE. Please help.
From Javascriptkit.com:
click() – Executes a click on a element as if the user manually clicked on it. In most browsers, click() only works on form INPUT elements that’s non “submit” or “reset”. It can’t be used to simulate a click on a link or form submit button.
I have verified that this is the reason why it’s not working for you. Also, I had to change the following line:
to
Will update with a way to do this in jQuery.
EDIT:
Here’s how you do it in jQuery. Essentially you bind a click event and then call click() on the element. This simulates the action since there is no standard way of executing clicks on hyperlinks.