I have a page which contains an iframe to another page, what I am looking for is to click something inside the iframe, which can then access or more specifically hide a div in the parent. I know some JQuery but for some reason just sit there blankly at the code editor when I try to write it on my own
-
1Are the page and the iframe hosted on the same subdomain?– rekireJul 30, 2012 at 8:34
-
yes they are, I have access to both pages if the code needs to go in either– Andrew MorrisJul 30, 2012 at 8:34
-
@Andrew Morris The code acces is not the problem – but browsers do not allow JavaScript actions across different domains. And those domains must match exactly (even the protocol must be the same – you can't handle https site via JS from an http site)– feeelaJul 30, 2012 at 8:43
-
@feeela, I read about the same domain policy and I knew the pages wouldn't be restricted by this, I just forgot to mention it in the question. Thanks anyway though– Andrew MorrisJul 30, 2012 at 8:53
Add a comment
|
3 Answers
try this in iframe:
$('#DIVtobehidden', window.parent.document).hide();
-
-
This answer helped me solve a similar problem I was having, adding custom enhancements to an old 3rd-party web application - but it was not the complete solution. In my case I needed to call .trigger('someevent') on the element in the parent frame, and it just wasn't working - there appeared to be no handler for the event in the JQuery data structures. Instead, I had to resort to the rather hacky-looking construct: window.parent.$("myselector").trigger("someevent") which despite being ugly, at least has the benefit of working!– LoopholeNov 11, 2020 at 6:32
$("#element-In-Iframe").on('click', function() {
$('#element-in-parent-window', window.parent.document).hide();
});
-
@CrystalMiller - It used to work three years ago, note that it's also exactly the same as the accepted answer. These days JSFiddle is running it's own iframes from a shell subdomain, so the same-origin policy kicks in.– adeneoJul 8, 2015 at 1:34
If both pages are on the same (sub)domain you should be able to access the parent window with:
window.parent.document.getElementById('divId')
Well this is without jQuery but should work