gpt4 book ai didi

javascript - # 在页面加载 URL 后自动出现

转载 作者:行者123 更新时间:2023-12-05 07:48:36 24 4
gpt4 key购买 nike

如果您访问链接http://www.feriebolig-europa.dk/ ,页面加载后将显示尾随的/#。

这基本上是这个脚本的问题:

<script type="text/javascript" src="https://p14943.atraveo.com/index.php?controller=partnerintegration&action=default&width=1080&height=1000&lg=da&cur=DKK&versionname=Spanien_landingpage"></script>

由 3 个不同的 javascript 组成。影响脚本是:

http://p14943.atraveo.com/website/static/js/partnerFrame.js

源脚本:

<script type="text/javascript">
window.addEventListener("message", function(a) {
if (!("website" !== a.origin.substr(a.origin.length - 7) && ".atraveo.com" !== a.origin.substr(a.origin.length - 12) && ".atraveo.de" !== a.origin.substr(a.origin.length - 11) && ".atraveo-test.de" !== a.origin.substr(a.origin.length - 16)))
if ("scrollUp" == a.data) window.location.hash = "atraveo", window.location.hash = "";
else if ("scrollToFrame" == a.data) {
if (a = document.getElementById(atraveoFrameId)) a = a.offsetTop, document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop =
a : document.body.scrollTop = a
} else document.getElementById(atraveoFrameId).style.height = a.data + "px"
}, !1);
</script>

下一行是更具体的可能原因:

if ("scrollUp" == a.data) window.location.hash = "atraveo", window.location.hash = "";

如果我添加:

window.location.href.replace('#', '');

问题得到解决。

但是,由于我无法控制第 3 方网站,我想知道是否可以从我这边做些什么?

最佳答案

try {
//check wether it's working + initial cleanup
history.replaceState( history.state, document.title, location.href.replace(/#$/,"") );

//if the code didn't throw yet, add the event-listener
addEventListener("hashchange", function(){
var href = location.href, regex = /#$/;
if(regex.test(href))
history.replaceState( history.state, document.title, href.replace(regex, "") );
}, false);
}catch(err){}

history-manipulation 可能在某些浏览器中不可用,这就是为什么我将代码包装在 try-catch-block 中并触发 history.replaceState()

另一方面,如果此代码在较旧的浏览器中失败,尾随的 # 可能没有吸引力,但这不是关键,需要不惜一切代价修复。

关于javascript - # 在页面加载 URL 后自动出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38276504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com