gpt4 book ai didi

javascript - document.location 重定向循环

转载 作者:行者123 更新时间:2023-12-03 08:16:20 24 4
gpt4 key购买 nike

我有 3 个不同的 index.html 类型页面,用于不同的屏幕尺寸:index.html 适合 > 1280,indexsm.html 适合 <= 1024,indexmed.html 适合 <= 1280。

我想我可以用下面的 JS 来解决这个问题:

if  (screen.width <= 1024)
{
document.location = "indexsm.html"
}
else if (screen.width <= 1280)
{
document.location = "indexmed.html"
}
else
{
document.location = "index.html"
}

在 1024 或更少的屏幕上打开 index.html,重定向工作正常并重定向到indexsm.html。在 1280 的屏幕上打开index.html,重定向工作正常并打开indexmed.html。在1920(例如)的屏幕上打开index.html,我的jacascript循环并不断地重新加载index.html页面。

有人有办法解决这个问题吗?我已经搜索了几个小时,但一直找不到治疗方法。

最佳答案

假设此脚本位于 in index.html 中,为什么要重新设置位置(并因此重新加载页面)?

if  (screen.width <= 1024) 
{
document.location = "indexsm.html"
}
else if (screen.width <= 1280)
{
document.location = "indexmed.html"
}

// else we're already where we need to be, so stop

关于javascript - document.location 重定向循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33923293/

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