Get element from iframe and click it - javascript

I want to get an element from an iFrame and click it. Getting the element succeeded. Unfortunately i cant get the element within the iframe clicked. Anyone with a solution?
Thanks!
$(document).ready(function(){
var frame = $('#f314dbebb8'); //ID from the frame
console.log(frame);
});

Is this what you are trying with
document.getElementById('iframeResult').contentWindow.document.getElementById('buttonId').click()
where iframeResult is Id of iframe and buttonId is Id of element to be clicked ??

Do you mean you are trying to "click it" programmaticly, like via JavaScript? Or a user interaction like with your mouse and cant?
If you are trying via JavaScript its not going to work. Look up something called "same origin security". JavaScript cannot access other pages load via an iframe for security purposes. http://javascript.info/tutorial/same-origin-security-policy

Have you tried document.getElementById('elementid').click();?

Related

Get value tr of id inside Iframe using Jquery or Javascript [duplicate]

Firstly both the parent page and the iframe are hosted on the same server (my localhost on WAMP) so same origin policy should not be an issue here.
I can't get the trigger to work. My iframe has the id of iframe.
$(window).load(function(){
//iframe ad hovers
$('#iframe').contents().find('body div').click(function(){
alert('do something here');
});
});
what am i doing wrong?
I'm not sure the browser's going to propagate a "click" event from the window context of the <iframe> out to the containing window. Does the document loaded into the <iframe> have its own copy of jQuery? If so, you can try this:
$('#iframe').contents().$.find('body div').click(function(){
alert('do something here);
});
That change makes the jQuery code in the <iframe> window handle the event.
Well I think that #jAndy is right and that should work as is - but you have to make sure the document in the frame is loaded. Try this:
$('#iframe').load(function() {
$(this).contents().find('body div').click(function() { alert("hi"); });
});
Your code is not wrong; i think the only mistake you are doing is that you are asking for the iframe's content on the parent page load which means at this stage the iframe has not been loaded yet.
Try my quick example http://jsfiddle.net/UFM44/4/
It is same as your code but i trigger it after clicking on "now" link. if you then click on the logo you can see the alert() message.

jquery selector from iframe content to object content

I need the following:
I got a html document, in which I have an iframe and an object. Both, the iframe and the object contain separat html files.
Now I want to click a link in the iframe, and this should affect the links inside the object to hide.
How do I use jQuery selectors to select the links in the object html file?
Structure:
<html file parent>
<iframe> html site 1 with link to click</iframe>
<object> html site 2 with links to affect </object>
<html file parent>
Thanks in advance!
This is not possible if the domain of the iframe is different from that of your .
This is a javascript restriction.
For this to possible you need to have control on the url loaded in the iframe.
If it is of same domain then you can probably do that.
If you have control over the iframe's url try this.
First, have a look at window.postMessage. Using this, you may send an event from your iframe to the window parent target. Listening for that event in the window parent (when something in your iframe changed), you will then be able to access any element inside the object tag using a syntax like this:
$('object').contents().find('linkSelector')
Give your iframe an id, let's say myIframe:
<iframe id="myIframe"> html site 1 with link to click</iframe>
Get a reference to the iframe:
var myIframe = document.getElementById('myIframe');
Post a message from iframe:
myIframe.contentWindow.postMessage('iframe.clicked', 'http://your-domain.here.com');
Handler for iframe change:
var handleIframeChange = function(e) {
//
if(e.origin == 'http://your-domain.here.com') {
// Get reference to your `object` tag
var objContent = $('object').contents();
// For instance, let's hide an element with a class of `link-class-here`
objContent.find('.link-class-here').hide();
}
}
Listen on parent window for the event sent by the iframe:
window.addEventListener('iframe.clicked', handleIframeChange, false);
Haven't tested it right now (did this in the past, when I had control over iframe) but it should work, but as I said, only if you can have control over the iframe.

Close ifreame using java script

How do i close the iframe using javascript.I found some method to do that using
window.parent.close();
but this method close the whole tab.But what I need is only close the iframe.Any one got a solution for that??
An iframe isn't a window, it can't be closed. You may hide it using
$(theIFrame).hide();
or remove it if you won't use it again :
$(theIFrame).remove();
If you want to hide or remove the iframe from a script inside the iframe document, you must get the relevant element first. You can do this :
var iframe = $(parent.document).find('iframe').filter(function() {
return this.src==location.href;
});
iframe.remove();
If you don't do this from the iframe, you should have some kind of reference. Or you may do it indiscriminately as
$('iframe').remove();
You cant close an iframe.
you must remove the node with elem.removeChild(elem_iframe)

GetElementById form a "master" inside an iframe

Ok this is in javascript, so I have a iframe inside a contente place holder of a master page.
Something like this.
1-MasterPage
2-contentePlaceHolder
3-Iframe
When i'am in the iframe how do I get a control with "GetElementById" from the masterPage??
Unfortunately, you cannot. Your iFrame is unaware of the master page holding it.
If both your parent page and iframe src are on the same domain then using JQuery you can do something like this from the iframe's src page:
$(parent.document).find('#myElement').hide('slow');
Try this :
Provided that masterpage and iframe src belong to same domain , let's say you have a button in iframe , on 'onclick' event of that button write this code :
var P = parent.document.getElementById("element_id") ;

Selecting an element in iframe with jQuery

In our application, we parse a web page and load it into another page in an iframe. All the elements in that loaded page have their token IDs. I need to select the elements by those token IDs. Means - I click on an element on the main page and select corresponding element in the page in the iframe. With the help of jQuery I'm doing it in the following way:
function selectElement(token) {
$('[tokenid=' + token + ']').addClass('border');
}
However with this function I can select the elements in the current page only, not in the iFrame. Could anybody tell me how can I select the elements in the loaded iFrame?
Thanks.
var iframe = $('iframe'); // or some other selector to get the iframe
$('[tokenid=' + token + ']', iframe.contents()).addClass('border');
Also note that if the src of this iframe is pointing to a different domain, due to security reasons, you will not be able to access the contents of this iframe in javascript.
Take a look at this post: http://praveenbattula.blogspot.com/2009/09/access-iframe-content-using-jquery.html
$("#iframeID").contents().find("[tokenid=" + token + "]").html();
Place your selector in the find method.
This may not be possible however if the iframe is not coming from your server. Other posts talk about permission denied errors.
jQuery/JavaScript: accessing contents of an iframe
when your document is ready that doesn't mean that your iframe is ready too,
so you should listen to the iframe load event then access your contents:
$(function() {
$("#my-iframe").bind("load",function(){
$(this).contents().find("[tokenid=" + token + "]").html();
});
});
If the case is accessing the IFrame via console, e. g. Chrome Dev Tools then you can just select the context of DOM requests via dropdown (see the picture).
here is simple JQuery to do this to make div draggable with in only container :
$("#containerdiv div").draggable( {containment: "#containerdiv ", scroll: false} );

Categories