I have an html page with iframe window in it. I want a variable to capture value as 0 when URL of iframe remains the same as initial and variable as 1 when there is a navigation within iframe.
Asked
Active
Viewed 3,782 times
1 Answers
1
Maybe something like this:
var clicked = 0;
$('iframe').load(function(){
var iframe = $(this).contents();
//notices if a tag is clicked within iframe
iframe.find("a").click(function(){
clicked = 1;
});
});
Or you could mean something like this(?):
var clicked = 0;
$('iframe').load(function(){
//store URL
var src = $("a").attr('src');
var iframe = $(this).contents();
//notices if a tag is clicked within iframe
iframe.find("a").click(function(){
//has the URL changed?
if(src != $(this).attr('src')){
clicked = 1;
}else{
clicked = 0;
}
});
});
Final tested script with .link
being the link inside the iframe:
var clicked = 0;
$(document).ready(function() {
$('iframe').load(function(){
var iframe = $(this).contents();
var src = iframe.find(".link").attr('href');
//notices if a tag is clicked within iframe
iframe.find(".link").click(function(){
//has the URL changed?
if(src != iframe.find(".link").attr('href')){
clicked = 1;
}else{
clicked = 0;
}
console.log(clicked);
});
});
});

maxisme
- 3,974
- 9
- 47
- 97
-
$('iframe').load(function(){- would work when the iframe loads..but i want the script to run after there is some naviagtion happens...the main idea is i have an html page where i want to enable a button when any kind of naviagtion happens within iframe. – Arpana May 11 '15 at 16:06
-
`iframe.find("a").click(function(){` notices if you click any `a` tag **after** the `iframe` loads. Have you tested my code? – maxisme May 11 '15 at 16:57
-
this is the error i get at console-"Uncaught SecurityError: Failed to read the 'contentDocument' property from 'HTMLIFrameElement': Blocked a frame with origin "null" from accessing a frame with origin "http://www.bing.com". The frame requesting access has a protocol of "file", the frame being accessed has a protocol of "http". Protocols must match." – Arpana May 12 '15 at 08:03
-
-
[this](http://stackoverflow.com/a/22625988/2768038) is a good answer to what might be happening in your case – maxisme May 13 '15 at 14:40