如果url有查询字符串,设置cookie

javascript if url has query string, set cookie

本文关键字:设置 cookie 字符串 查询 url 如果      更新时间:2023-09-26

如何设置cookie,如果url有一个查询字符串'?no_redirect=1'?

我没有得到任何正在设置的cookie。目前我有

function cookieQuery() {
    var referrer = "//domain.local";
    if (document.referrer) {
        var no_redir = 'no_redirect';
        var url = window.location.href;
        if(url.indexOf('?' + no_redir) != 1)
            return true;
        else if(url.indexOf('&' + no_referral) = 1)
           $.cookie('no_referral', 'true');
                }
            }

您在这行使用的是赋值操作符,而不是比较操作符:else if(url.indexOf('&' + no_referral) = 1)

将其更改为else if(url.indexOf('&' + no_referral) == 1),假设其余代码是正确的,它应该可以工作。