根据url参数设置Cookie [英] Set a Cookie based on url Parameter

查看:304
本文介绍了根据url参数设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当用户通过我们的某个联属网络链接点击并在我们的网站上停留时,我需要设置一个Cookie,并在网址中加入src = uni。网址格式如下:

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:

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

创建cookie的函数:

Function to create cookie:

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

有人可以告诉我在创建这个基于Cookie的关于查询参数?

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

推荐答案

这里有一些事情:

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

1)使用 location.search 缩小范围,不必要,但减少出错的空间。

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

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

2) Use !== -1 to test the indexOf method. indexOf returns "-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".

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

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

string uni如果这是你正在寻找的,而不是一个名为 uni 的变量。如果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.

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

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

这篇关于根据url参数设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆