为什么第二个脚本阻止第一个脚本重定向,我该如何解决?



除了重定向之外,一切正常。如果我删除第二个脚本,重定向有效。我需要做些什么才能让两者正常工作吗?

<script>
function check_frame() {
if( top === self ) { // not in a frame
location.href = "https://xyz.any"; // either the frameset or an error.
}
}
</script>
<script>
function check_frame() {
if( top !== self ) { //  in a frame
document.body.classList.add('framed');
}     
}
</script>
<body onLoad="check_frame()">
</body>
<style>
body {
opacity: 0;
transition: opacity 2s ease-in;
}
body.framed {
opacity: 1;
}
</style>

你不需要(实际上,这没有意义(同一个函数的两个定义。只需包装在单个实现中,如下所示:

function check_frame() {
if( top === self ) { // not in a frame
location.href = "https://xyz.any"; // either the frameset or an error.
}
else {
document.body.classList.add('framed');
}
}     

最新更新