18910140161

JavaScript-window.open(URL)方法再次重新加载页面&AGAN-堆栈溢出

顺晟科技

2022-10-18 12:48:07

51

我有一个问题,我需要一个功能/程序,当特定的屏幕到达它重新加载另一个窗口,但当我打开该窗口时,它会再次重新加载&;又。我正在使用window.open()方法打开其他链接,但如果你知道其他更好的解决方案,我准备讨论。

<代码>函数重定向(){设deviceWidth=document.documentElement.clientWidth;如果((设备宽度>;320)&;&;(设备宽度<;720 )) {手机();}Else if((设备宽度>;721)&;&;(设备宽度<;1199)) {片剂();}Else if(设备宽度>;1200) {桌面();}别的{console.log(";有问题";);}函数移动(){//window.location.replace(";./mobile.HTML";);let win=window.open('./mobile.HTML ',";_self";,";";,true)win.focus();console.log('移动');}功能表(){//window.location.replace(";./tablet.HTML";);let win=window.open(' tablet.HTML ',";_self";,";";,true)win.focus()console.log(' tablet ');}功能桌面(){//window.location.replace(./index.HTML);let win=window.open(' index.HTML ',_self,true)win.focus();console.log('桌面');}}重定向();

顺晟科技:

我得到了答案。问题是,我调用函数来打开一个新的HTML文件,即使我的窗口已经打开了该HTML文件,这导致它重新启动。例如:如果当前我在mobile.HTML中,那么我在调用它时犯了一个错误,即使它是打开的。



function redirect() {
    let deviceWidth = document.documentElement.clientWidth;

    if ((deviceWidth > 320) && (deviceWidth < 720 )) {
        mobile();
    }

    else if ((deviceWidth > 721) && (deviceWidth < 1199)) {

        tablet();
    }

    else if (deviceWidth > 1200)  {

        desktop();
    }
    else {
        console.log("something wrong");
    }






    function mobile() {
        // window.location.replace("./mobile.html");
        let win = window.open('./mobile.html',"_self","",true)
            win.focus();
            
            console.log('mobile');
        
    }
    function tablet() {
        // window.location.replace("./tablet.html");
        let win = window.open('tablet.html',"_self","",true)
            win.focus()
            
            console.log('tablet');
        
    }
    function desktop() {
        // window.location.replace("./index.html");
        

        let win = window.open('index.html',"_self","",true)
        win.focus();
        console.log('desktop');

        
    }
}
redirect();




这导致了它的递归。

我的第一个想法是检查这些功能是否也存在于反复加载的页面上。如果是这样,请在移动、平板电脑和index.HTML页面中删除对redirect()的调用。这将导致以递归方式调用redirect()。

如果您需要保留redirect()函数,则很可能需要考虑一些if语句。例如,您可以在重定向后获取引用标头,如果它来自这些页面中的任何一个,则可以中断循环。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航