javascript 根据 url 参数设置 Cookie

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29804219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:06:21  来源:igfitidea点击:

Set a Cookie based on url Parameter

javascripturlcookiesquery-stringaffiliate

提问by Programmermid

I need to set a cookie whenever a user clicks through one of our affiliate links and lands on our site with "src=uni" in the URL. The URLs will look something like this:

每当用户点击我们的附属链接之一并在 URL 中使用“src=uni”登陆我们的网站时,我都需要设置一个 cookie。URL 将如下所示:

http://www.myadmin.com?src=uni&utm_source=uni&utm_content=[publisher_ID]

http://www.myadmin.com?src=uni&utm_source=uni&utm_content=[publisher_ID]

Function to create cookie:

创建cookie的函数:

 function SetCookie() {
            var url = window.location.href;
            if(url.indexOf('?src' + uni) = 1)
                document.cookie="QueryCookie";
    }

Can somebody help me by telling where I am going wrong in creating this Cookie based on query parameters?

有人可以通过告诉我在基于查询参数创建此 Cookie 时出错的地方来帮助我吗?

采纳答案by bloodyKnuckles

A few things here:

这里有几件事:

function SetCookie() {
    var url = window.location.search;
    if(url.indexOf('?src=uni') !== -1)
        document.cookie="src=uni";
}

1) Use location.searchto narrow down your range, not necessary, but less room for error,

1)location.search用来缩小你的范围,不是必须的,但可以减少出错的空间,

2) Use !== -1to test the indexOfmethod. indexOfreturns "-1"if it does not find a match. And "0"if it finds a match at the beginning of the string. The string is "zero indexed" which means the first character in the string is in position "0".

2)!== -1用于测试indexOf方法。如果找不到匹配项,则indexOf返回“-1”。和“0”,如果找到匹配的字符串的开头。该字符串是“零索引”,这意味着字符串中的第一个字符在位置“0”。

3) Add the equal sign =along with your parameter name: src=.

3) 添加等号=和您的参数名称:src=

4) Also, use the string "uni"if that is what you're looking for, rather than a variable named uni. If "src"can be a variety of values, then we'll need to add some more logic to account for that.

4) 另外,如果您要查找字符串“uni”,请使用字符串“uni”,而不是名为uni. 如果“src”可以是各种值,那么我们需要添加一些更多的逻辑来解决这个问题。

5) And when assigning to document.cookieuse key/value pairs as in: key=value.

5)又分配给当document.cookie使用键/值对,如:key=value

回答by WakeskaterX

First thing you need to fix is:

您需要修复的第一件事是:

if(url.indexOf('?src' + uni) = 1)

should be (this checks that it exists at index 1):

应该是(这会检查它是否存在于索引 1):

if(url.indexOf('?src=' + uni) === 1)

or (this checks it exists at all)

或(这会检查它是否存在)

if(url.indexOf('?src=' + uni) !== -1)

Next, you need to set src to the uni and make it accessible to the entire site:

接下来,您需要将 src 设置为 uni 并使整个站点都可以访问它:

document.cookie="src="+uni+"; path=/; domain=.myadmin.com";

Adding the path=/ and domain=.myadmin.com will allow you to access the cookie at all paths on that domain, and the domain part lets it be accessible on all subdomains (i.e. www.myadmin.com as well as blog.myadmin.com, etc)

添加 path=/ 和 domain=.myadmin.com 将允许您访问该域上所有路径的 cookie,域部分允许它在所有子域上访问(即 www.myadmin.com 以及 blog.myadmin .com 等)

so all together:

所以一起:

    function SetCookie() {
        var url = window.location.href;
        if(url.indexOf('?src='+uni) !== -1)
            document.cookie="src="+uni+"; path=/; domain=.myadmin.com";
    }


Here is some basic info:

以下是一些基本信息:

http://www.w3schools.com/js/js_cookies.asp

http://www.w3schools.com/js/js_cookies.asp

Or the more in depth, accurate documentation:

或者更深入、更准确的文档:

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie

https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie