Set A Cookie Based On Url Parameter
Solution 1:
A few things here:
functionSetCookie() {
var url = window.location.search;
if(url.indexOf('?src=uni') !== -1)
document.cookie="src=uni";
}
1) Use location.search
to narrow down your range, not necessary, but less room for error,
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) Add the equal sign =
along with your parameter name: 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) And when assigning to document.cookie
use key/value pairs as in: key=value
.
Solution 2:
First thing you need to fix is:
if(url.indexOf('?src' + uni) = 1)
should be (this checks that it exists at index 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:
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)
so all together:
functionSetCookie() {
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
Or the more in depth, accurate documentation:
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
Post a Comment for "Set A Cookie Based On Url Parameter"