简体   繁体   English

防止在新标签/窗口中打开

[英]Prevent open in new tab/window

I have a website that has a music player in one of the corners.我有一个网站,其中一个角落有音乐播放器。 Because I didn't want the music to stop everytime the user changes part of the website, I used an iFrame for the content.因为我不希望每次用户更改网站的一部分时音乐停止,所以我使用 iFrame 作为内容。 So when someone clicks the menu buttons, all that changes is the source of the iFrame.所以当有人点击菜单按钮时,所有改变的只是 iFrame 的来源。

Now the only problem is if someone tries to open one of the sub-pages in a new tab/window.现在唯一的问题是,如果有人试图在新选项卡/窗口中打开其中一个子页面。 All they get is black text on a white background, with the content that was supposed to be in the iFrame.他们得到的只是白色背景上的黑色文本,其内容应该在 iFrame 中。

Is there any way to prevent this?有什么办法可以防止这种情况发生吗?

Oh, and the page this is about is www.sinjabe.com if that helps.哦,这个页面是www.sinjabe.com如果有帮助的话。

Thanks谢谢

The most logical solution (to me...) would be to detach the music player from the page;最合乎逻辑的解决方案(对我来说......)是将音乐播放器与页面分离; have the music player open in a different window / tab.让音乐播放器在不同的 window / 选项卡中打开。 You can have the music player in a pop-up window when the visitor of your site requests it / clicks on it.当您的网站访问者请求/点击它时,您可以在弹出窗口 window 中使用音乐播放器。

Other solutions like detecting if the content has a parent frame, will lead to multiple music players if someone opens a page in a new tab / window.其他解决方案,例如检测内容是否有父框架,如果有人在新标签/window 中打开页面,将导致多个音乐播放器。

You can alter your sub-pages to detect if they are loaded in an iframe like this:您可以更改子页面以检测它们是否加载到 iframe 中,如下所示:

(window.location != window.parent.location) ? true : false;

and then load or redirect to the full template as needed.然后根据需要加载或重定向到完整模板。

Put this on your sub pages:把它放在你的子页面上:

<script type="text/javascript">
    if (window.location == window.parent.location)
    this.window.location = 'main_site.html';
</script>

-- Give credit for this one @George Cummins for writting such a short conditional statment:) ——感谢@George Cummins 写了这么短的条件语句:)

This will redirect any direct incomming trafic on your subpages to your main page.这会将您子页面上的任何直接传入流量重定向到您的主页。 I must warn you though, that iframes for displaying such content are highly SEO unefficient, and URL useless in terms of history mapping (back button will not work) and link sharing, because the main URL doesn't change while you click on menu buttons.不过我必须警告您,用于显示此类内容的 iframe 的 SEO 效率非常低,并且 URL 在历史映射(后退按钮不起作用)和链接共享方面无用,因为主要的 URL 在您单击菜单按钮时不会改变.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM