How To Prevent Iframe Pages Being Inserted Into IE

2020-07-23 05:00发布

问题:

I'm having a lot of trouble with iframe pages being inserted into the browsing history of IE9. It's a pain because you have to click back several times in order to get to the previous page. What is the cause of this? Is it a bug in the browser or the page?

Edit: I realized I was using javascript to update the source of all iframes on the page to add the wmode=transparent attribute.

$('iframe').each(function () {
    var url = $(this).attr("src")
    $(this).attr("src", url + "?wmode=transparent");
 });

This was added to combat menu navigations were going behind YouTube videos. Is there a way to prevent IE from inserting these events into the history?

回答1:

An option would be to replace the iframe rather than update the source.

$('iframe').each(function () {
   var url = $(this).attr("src")
   //$(this).attr("src", url + "?wmode=transparent");

   url = url + "?wmode=transparent";

   var frm = document.createElement("iframe");
   frm.src = url;
   //copy attributes of this -> frm
   $(this).replaceWith(frm);
});

A history entry is made any time an iframe's src attribute is modified after it has been inserted into the DOM. If you aren't in control of the contents of the iframe (a third party iframe), then if they modify the location of their page in any way a history entry will be created.



回答2:

I don't know about all circustances but at finance.yahoo.com a link to facebook's like.ph is sometimes inserted in the back button history. It is caused by a script on the page. It happens when the facebook link becomes visible on the web page. That triggers the function that creates the link in the back button history. The user does not have to click on anything to cause this to happen.



回答3:

instead of

$(this).attr("src", url + "?wmode=transparent");

do

$(this).get(0).contentWindow.location.replace(url + "wmode=transparent");