【问题标题】:Run function once after redirecting重定向后运行一次函数
【发布时间】:2014-10-28 20:12:28
【问题描述】:

我正在尝试将访问网站的用户重定向到移动网站。这是我到目前为止所做的,但问题是每次页面加载时该函数都会继续运行。页面加载后调用这些函数。我是 JavaScript 初学者。

function redirectPage() {
    var runOnce = 0;

    if (runOnce == 0 && windowWidth <= 767){
        runOnce = 1;
        window.location.assign("example.net/mobile-site");
    }

    else if (runOnce == 0 && windowWidth >= 767){
        runOnce = 1;
        window.location.assign("example.net/regular-site");
    }
}

更新

这就是我所做的,但到目前为止浏览器一直在一次又一次地加载。

var windowWidth = 0;

$(document).ready(function(){
    checkBrowserWidth();
    redirectPage(); 
});

function checkBrowserWidth() {      
    windowWidth = window.outerWidth;
}

function redirectPage() {
    if (typeof(Storage) != 'undefined') {
        // Store value 
        localStorage.setItem('runOnce', 0);
    }
    var runOnce = localStorage.getItem('runOnce');

    if (runOnce == 0 && windowWidth <= 767){
        localStorage.setItem('runOnce', 1);
        window.location.assign("example.net/mobile-site");
    }

    else if (runOnce == 0 && windowWidth >= 767){
        localStorage.setItem('runOnce', 1);
        window.location.assign("example.net/regular-site");
    }
}

【问题讨论】:

  • javascript 在页面加载时运行,因为 javascript 在页面加载时按照它在页面上出现的顺序执行。当您更改 window.location 时,您正在加载一个不同的页面,并且该页面上的 javascript 将在页面加载时执行。
  • 我做的是做一个饼干。如果 cookie 存在,则代码不运行,如果不存在(清除缓存或过期),则运行并创建一个新 cookie。
  • @user3757910 你想通过检查Storage 未定义来完成什么?如果您检查那里发生的事情,您可以找出问题的根源。

标签: javascript function redirect runonce


【解决方案1】:

您的方法存在几个问题。

范围

JavaScript 有function scope。这意味着 runOnce 将始终是 undefinedredirectPage 函数之外。所以每次通话都会留下runOnceundefined

console.log(window.setSomething); // undefined
function scopeExample() {
  var setSomething = 'something';
}
console.log(window.setSomething); // undefined

如果你想保存一个全局变量,你需要在一个全局范围内设置它,比如window

// this will be set on a global-basis, however it will not affect the next request as 
// explained in the next section
window.runOnce = 0;
function redirectPage() {    
    if (window.runOnce == 0 && windowWidth <= 767){
        window.runOnce = 1;
        window.location.assign("example.net/mobile-site");
    }

    else if (runOnce == 0 && windowWidth >= 767){
        window.runOnce = 1;
        window.location.assign("example.net/regular-site");
    }
}

脚本生命周期

将每个页面加载完全想象为一个单独的应用程序。除非您愿意,否则它不知道先前的请求。您需要将其保存在 cookie 或像 localStorage 这样的客户端存储中。

function redirectPage() {
    var runOnce = localStorage.get('runOnce');

    if (runOnce == '0' && windowWidth <= 767){
        localStorage.set('runOnce', '1');
        window.location.assign("example.net/mobile-site");
    }

    else if (runOnce == '0' && windowWidth >= 767){
        localStorage.get('runOnce', '1');
        window.location.assign("example.net/regular-site");
    }
}

【讨论】:

  • runOnce 总是设置为 0 并不是真的,因为在评估 if 语句后,它将设置为 1。然而,值或 runOnce 将在redirectPage 函数,设置window.location后页面加载时会被redirectPage函数重置为0。
  • @Zack 你是对的。这就是我试图传达的(显然失败了)。
  • 您的编辑很好。我想你现在的意思很清楚了,一开始措辞听起来不对。
猜你喜欢
  • 2022-06-15
  • 2021-07-21
  • 2023-04-09
  • 1970-01-01
  • 2013-11-12
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多