Get browser URL from two level depth iframe - javascript

There is an iframe in an other iframe. I would like to get the browser's URL (in example: http://test) from the second iframe.
//browser URL: http://test <-- I need this URL
<iframe src="http://level1">
<iframe src="http://level2">
<!-- some content -->
</iframe>
</iframe>
I tried document.referrer but it gives back the first iframe's URL:
<iframe src="http://level1">
<iframe src="http://level2">
console.log(document.referrer); //It gives back http://level1
</iframe>
</iframe>
UPDATE:
The top.location.href doesn't work because there are different domains. Different the browser, the first and the second iframe domain. (our website - adserver - third-party website)

As said it is in general not possible from cross-domain iframes, but in the case of Chrome you can use window.location.ancestorOrigins that will give you the "top level url" and a list of all the domains if you are in several iframes no matter if they are cross-domain or not.

Related

how to close iframe if iframe page src redirects to specific url

i am using a simple iframe html code where i am embedding the jitsi meet url. now the problem is after finishing a meeting with jitsi it redirects to its close3.html page. i dont want to show this in my iframe. i want to close my iframe when it redirects to close3.html page. i have tried with sandbox. but after using sandbox camera and microphone access turns disable.
here is my iframe code
<iframe
title="Open identification process"
src="https://meet.jit.si/4b7562c798c20e3dd523b2298cad5327"
frameBorder="0"
width="600"
height="800"
allow="camera; microphone"
id="iframe"
onLoad="alert('Test');"
/>
You will face the Same Origin Policy when you try to do.
You can't access an <iframe> with different origin using JavaScript,
it would be a huge security flaw if you could do it.
Even so, you could use that method:
document.getElementById("iframe_id").contentWindow.location.href
Further Information

Iframe isn't visible on Chrome browser

I have an iframe issue with the Chrome browser. In http://bulbanner.cf has the following iframe code:
<iframe src="http://bulbanner.cf/script.html" width=468 height=60 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling="no" title="BulBanner - banner exchange"></iframe>
When you paste the code in another website, 468x60 sized banners are starting to visualize, because in the http://bulbanner.cf/script.html page has a rotator which shows random 468x60 sized banners. Shortly, something like this:
With Firefox everything is fine, but with Chrome very often the iframe doesn't work. Look the result with Chrome:
I think this is a security issue from Chrome browser, however is there any way to resolve it?
I have rights to change the iframe code or I can put some code in the http://bulbanner.cf/script.html page which is visualizing via the above mentioned iframe.
In this case the X-Frame-Origin header is generated by Google’s support page. So you can’t change that.
The website you want to make an iFrame has a .htaccess code that blocks iFrame request.
Like if you insert this code Header set X-Frame-Options DENY to your website’s .htaccess then any iFrame requests will be blocked.

Detect if an iframe is loaded with JavaScript (including those that fail from Cross origin)

I have an url viewer in my webpage that uses iframes and I would like to detect when the iframe is loaded. I don't need to know if the iframe fails to load. What I want is knowing when it is really loaded.
I have tried with:
$(iframe).on('load', ...)
but it gets triggered even when the browser throws a "Refused to display ...":
// example
<iframe src="http://google.com"></iframe>).
so I thought I could check if the document is empty or not using iframe.contentDocument but it seems that it isn't always accessible:
// example with a youtube link
<iframe width="854" height="480" src="https://www.youtube.com/embed/-7EVT6IyFxk" frameborder="0" allowfullscreen></iframe>
Any ideas ?

Cross Browser Automatically Click Link On Page Load with Javascript

I have a simple html page with an iframe image link. After the page loads I want the link to be clicked automatically. I need the link to be clicked instead of doing a meta refresh so thats out of the question. I searched around and It seems like FireFox does not support any methods to do so...There has to be a cross browser solution. Here is what I have so far but it wont work...why?
<script type="text/javascript">
window.onload=functionName;
$(document).ready(function(){
$('#clicked').trigger('click')
});
</script>
<div id="clicked">
<iframe src="http://mysource.com" width="40" height="10" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>
</div>
Add # before the id value
$('clicked').trigger('click');
to
$('#clicked').trigger('click')
If the iframe is on your domain then you can access its contents via jquery selectors.
Otherwise you're out of luck - blocking cross domain iframe interactions is a feature of same-origin policy that exists to protect users.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#Scripting

How to access frames within a frame?

<iframe name="aa">
<iframe name="bb">
</iframe>
<iframe name="cc">
</iframe>
</iframe>
(assume that iframes shown above have all the required attributes.)
Can I get subframes of frame with name="aa" in native javascript?Browser renders them all, no problem.
I was trying frame.frames to get the array of inner frames, where frame is frame object for outer frame i.e, "aa". But it is not working.
Is it even possible ?
Any help ???
Many thanks.
You can do this, however as each iframe is technically loading another page, you would have to code it into the actual webpage that your first iframe is loading.
To further explain...
<iframe name"aa"></iframe> <!-- this is coded into the first page calling the page below //-->
<iframe name="bb"></iframe> <!-- coded into the second page calling another page //-->
<iframe name="cc"></iframe> <!-- coded into the second page calling anothe page //-->
Remember, iframes are just "windows" into other pages.

Categories