Opening an iframe source in a new tab if it tries to redirect the user

天涯浪子 提交于 2019-12-11 03:32:06

问题


It's a known fact that a lot of websites use javascript codes to prevent from browsing them using iframes. I would like then my iframe to behave like this: if the site doesn't have such a code and isn't trying to redirect the user, its content should be shown in the iframe. But if it's trying to redirect, it should be opened in a new tab (just like links with target="_blank"). Is it possible to use onUnload or any other techniques to do such a thing?

Thanks in advance,
Michael.


回答1:


This will be extremely tough and complex and kludgy to achieve, if it's possible at all. It would require to predict the target site's behaviour before navigating to the link. On second thought, this may in fact be possible using the onbeforeunload event but I expect a lot of trouble with pop-up blockers and the like.

I am assuming you have external links, and would like to show some "return to original site" bar in order to not lose visitors.

What I would do as a simple workaround is open the site in a new target="_blank" window in both cases, and have your "return to site" link close the window using window.close().



来源:https://stackoverflow.com/questions/3952848/opening-an-iframe-source-in-a-new-tab-if-it-tries-to-redirect-the-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!