简体   繁体   English

如何阻止 javascript 重定向页面?

[英]How do I stop javascript from redirecting the page?

We run a few popular websites and are having issues with ads redirecting mobile users to other websites and app stores when the page loads.我们运行了一些流行的网站,但在页面加载时,广告将移动用户重定向到其他网站和应用程序商店时遇到了问题。 Is there a way to block javascript from changing the url?有没有办法阻止javascript更改url?

I have a couple ideas but haven't made anything work yet:我有一些想法,但还没有做任何工作:

  • use beforeunload to stop from directing away from the page but I need away to tell if the user meant to leave by clicking on something使用 beforeunload 停止引导离开页面,但我需要离开来判断用户是否打算通过单击某些内容离开

  • use window.MutationObserver to monitor DOM but I'm not sure this can be used to monitor changes to window.location使用 window.MutationObserver 来监控 DOM 但我不确定这可以用来监控 window.location 的变化

Anyone have a solution?有人有解决方案吗?

UPDATE更新

I tried the suggestion below to add the "sandbox" attribute to the iframe that holds the ad.我尝试了下面的建议,将“沙箱”属性添加到包含广告的 iframe。 On my test page it did stop my page from running the redirect but it didn't stop the ad redirects when I made this live!在我的测试页面上,它确实阻止了我的页面运行重定向,但是当我将其上线时并没有停止广告重定向! So I'm guessing they are redirecting the page with another method that gets through the sandbox block.所以我猜他们正在使用另一种通过沙箱块的方法重定向页面。 Anyone have any ideas how they could be doing this?任何人都知道他们如何做到这一点?

example iframe:
<iframe sandbox="allow-forms allow-pointer-lock allow-scripts" ..

example iframe html (redirect):
window.top.location.href = "example.com";

If the ads are in an iFrame add the sandbox attribute to the iFrame tag.如果广告位于 iFrame 中,请将sandbox属性添加到 iFrame 标记。 Per w3schools it will prevent the following...根据 w3schools 它将防止以下...

  • treat the content as being from a unique origin将内容视为来自独特的来源
  • block form submission阻止表单提交
  • block script execution阻止脚本执行
  • disable APIs禁用 API
  • prevent links from targeting other browsing contexts防止链接针对其他浏览上下文
  • prevent the content to navigate its top-level browsing context阻止内容导航其顶级浏览上下文
  • block automatically triggered features (such as automatically playing a video or automatically focusing a form control)阻止自动触发的功能(例如自动播放视频或自动聚焦表单控件)

NOTE - this is a HTML5 feature, and won't work in older browsers注意- 这是一个 HTML5 功能,在旧浏览器中不起作用

When using standard javascript tags, the easiest solution is to increase your mobile tag floors above ~$0.45 (you should not need to go higher than $0.55).使用标准 javascript 代码时,最简单的解决方案是将您的移动代码底价提高到 0.45 美元以上(您不需要高于 0.55 美元)。

For the most part we see mobile redirects mostly come in on 300x250 sizes, but have also seen it on 728x90 particularly when displayed on mobile (as 728x90 is more geared for desktop and demand for that size is extremely low on mobile meaning the bad ads win by paying a cheaper price).在大多数情况下,我们看到移动重定向大多采用 300x250 尺寸,但也看到它出现在 728x90 上,尤其是在移动设备上显示时(因为 728x90 更适合桌面设备,而移动设备上对该尺寸的需求极低,这意味着不良广告获胜通过支付更便宜的价格)。

If you are using prebid.js, other header bidding or multi-bid tags, in most cases you will have the ability to programmatically inspect the ad payload before you append the unit code to the page.如果您使用 prebid.js、其他标头出价或多出价标签,在大多数情况下,您将能够在将单元代码附加到页面之前以编程方式检查广告负载。

This provides an excellent opportunity to regex / search the ad payload string for code that sets window.location, and enables the developer to take specific actions when detected (such as bypass the bad ad and instead display a trusted remnant solution or house ad).这提供了一个极好的机会来正则表达式/搜索设置 window.location 的代码的广告负载字符串,并使开发人员能够在检测到时采取特定操作(例如绕过不良广告并改为显示受信任的剩余解决方案或自家广告)。

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

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