Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking an anchor inside an iFrame behaves differently in Firefox and Chrome

I have the following code in jsBin: http://jsbin.com/iRoROvu/1/edit

It basically has an anchor inside an iframe. If you click the link you will see nothing happens in Chrome but in Firefox it just goes blank.

Here's the same code in jsFiddle: http://jsfiddle.net/LbNwd/

JavaScript:

 var previewFrame = document.getElementById('preview');
    var preview =  previewFrame.contentDocument ||     previewFrame.contentWindow.document;     
    preview.open();
    preview.write("Hello World!<br/><a href='#'>Click me!</a>");
    preview.close();

HTML:

<iframe id="preview"></iframe>

In this case, if you use Firefox, it creates another iframe inside the existing iframe...and keeps on doing it...like 'Inception'. But the same code works fine in Chrome.

Can anyone tell me why?

like image 402
Gandalf Avatar asked Oct 15 '13 18:10

Gandalf


People also ask

What are legitimate uses of iframes?

What is an iframe, and when do you use it? Developers mainly use the iframe tag to embed another HTML document within the current one. You may have crossed paths with it when you had to include a third-party widget (like the famous Facebook like button), a YouTube video, or an advertising section on your website.

Can I hide content inside an iframe from an external domain?

Can I hide content inside an iframe from an external domain? Yes totally doable. Once you assign the parameter to a var, you could then do anything you want… like a hide() on an element.

How do you know if a page is loaded as an iframe?

In short, to check if a page is in an iframe, you need to compare the object's location with the window object's parent location. If they are equal, then the page is not in an iframe; otherwise, a page is in an iframe.


1 Answers

Yes. You should use href='javascript:void(0);' instead href='#'

OR

You can use <a href='#' target="preview">Click me!</a> instead.

I think adding target="preview" also should solve your problem.

Just try it once & let us know wether its working or not.

like image 60
user1199842 Avatar answered Nov 15 '22 00:11

user1199842