How to track parent url in iframe using javascript?

Yes, locating the parent page's URL is not accustomed if the iframe and the initial folio are not in the aforementioned (sub)domain. However, if you separate load the URL of the capital folio (i.e. the browser URL), you can try this:


var url = (window.location != window.parent.location) ?

I have searched about and I know that this is reasonable if my iframe page is on a different domain, as that is cross-site scripting. But everywhere I've read assumes that if the iframe page is on the same domain as the parent page, it should work if I do for example:
  • parent.document.location
  • parent.window.document.location
  • parent.window.location
  • parent.document.location.href
 
Yes, accessing the

var url = (window.location != window.parent.location) ? document.referrer : document.location.href;

 parent page's URL is not allowed if the iframe and the main page are not in the same (sub)domain. However, if you just need the URL of the main page (i.e. the browser URL), you can try this:
Example
<script>

$(document).ready(function() {
setTimeout(function() {
var url=window.parent.location.href;
var split=url.split("/");
var furl=split[0]+"//"+split[1]+split[2]+"/"+split[3];

$("#website_url").val(furl);

var url_string = new URL(url);
var keyword=url_string.searchParams.get("keyword");

if(keyword !=''){
$("#keyword").val(keyword);
}
var campaign=url_string.searchParams.get("campaign");
$("#campaign").val(campaign);
$("#medium").val(parent.document.referrer);
}, 5000);<script>
$(document).ready(function() {
setTimeout(function() {
var url=window.parent.location.href;
var split=url.split("/");
var furl=split[0]+"//"+split[1]+split[2]+"/"+split[3];

$("#website_url").val(furl);

var url_string = new URL(url);
var keyword=url_string.searchParams.get("keyword");

if(keyword !=''){
$("#keyword").val(keyword);
}
var campaign=url_string.searchParams.get("campaign");
$("#campaign").val(campaign);
$("#medium").val(parent.document.referrer);
}, 5000);
</script>

 Important Links https://stackoverflow.com/questions/3420004/access-parent-url-from-iframe 

No comments