I want to change cookie from the marked 'object' in JS.
Can you help me?? Thanks
My code JS:
function tryCookies(src, domain) {
if (src == "t:s") {
setCookie("$dtype", "", -1, domain);
setCookie("$dtype", src, 1, domain);
}
};
function setCookie(name, value, days, domain) {
var date = new Date();
date.setDate(date.getDate() + days);
document.cookie = name + "=" + escape(value) + ";expires=" + date.toGMTString() + ";domain=" + domain + ";path=/";
};
My code HTML:
<object class="cd" data="WebSite" type="text/html" onload="tryCookies('t:s', 'cdiscount.com.co');"></object>
Related
This is the code I'm using. Please help me to set cookie for a day! I want to show welcome page when a visitor come back after 24 hours.
<SCRIPT LANGUAGE="JavaScript">
<!-- hide from old browsers
function setCookie(name, value, expires, path, domain, secure) {
document.cookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
function setsplash() {
setCookie("splash", "1", "", "/");
}
//-->
</SCRIPT>
Your method setCookie() is defined as
function setCookie(name, value, expires, path, domain, secure)
The third parameter is the expiration time. But you're calling
setCookie("splash", "1", "", "/");
i.e. the expires parameter is "". If you want the cookie to expire after 24 hours, use something like this instead:
var expires = (new Date(Date.now() + 86400000)).toUTCString(); // 86400000 milliseconds is 24 hours
setCookie("splash", "1", expires, "/");
I'm trying to set a cookie that will expire at the end of the day. I've create this function :
function mnc(cname,cvalue)
{
var now = new Date();
var expire = new Date();
expire.setFullYear(now.getFullYear());
expire.setMonth(now.getMonth());
expire.setDate(now.getDate()+1);
expire.setHours(0);
expire.setMinutes(0);
//alert(expire.toGMTString() + " " + expire.toString());
var expires = "expires="+expire.toString();
alert(expires + "=> now =" + now);
document.cookie = cname + "=" + cvalue + "; " + expires +"; path=/";
}
On Fiddle : http://jsfiddle.net/MYs6b/
So, the alert box show me the good expiration date.
But, if I change the date on my computer by adding 1 or 100 days, i still have the same value in the cookie.
Why? I'm searching since 3 hours and i don't understand...
EDIT :
I've had an alert on "document.cookie" is empty
http://jsfiddle.net/MYs6b/2/
EDIT 2 :
I've add a better example of my problem. It's working on IE and FF but not on chrome :
http://jsfiddle.net/5h87M/1/
Try This: http://jsfiddle.net/MYs6b/1/
function mnc(cname,cvalue)
{
var now = new Date();
var expire = new Date();
expire.setFullYear(now.getFullYear());
expire.setMonth(now.getMonth());
expire.setDate(now.getDate()+1);
expire.setHours(0);
expire.setMinutes(0);
expire.setSeconds(0);
var expires = "expires="+expire.toString();
alert(expires + "=> now =" + now);
document.cookie = cname + "=" + cvalue + "; " + expires +"; path=/";
}
mnc("test", "123456");
Last update of chrome correcting this issue.
function createCookie(name,value,path) {
var expires = "";
var date = new Date();
var midnight = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 23, 59, 59);
expires = "; expires=" + midnight.toGMTString();
if (!path) {
path = "/";
}
document.cookie = name + "=" + value + expires + "; path=" + path;
}
I was trying to make a simple program that displays a welcome message if a cookie is set and if the cookie is not set it will display a prompt box, asking for the user name.
It doesn't work and I don't understand why. Can anyone tell me where is the problem in the code?
Here is the code:
<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(c_name,value,expiredays){
var exdate = new Date();
exdate.setDate(exdate.getDate()+expiredays);
if(expiredays==null)
document.cookie = c_name + "=" + escape(value) +"";
else
document.cookie = c_name + "=" + escape(value) + ";expires="
+exdate.toGMTString());
}
function getCookie(c_name){
if(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if(c_start!=-1)
c_start=c_start + c_name.length +1;
//index of the value's end
c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1)//if it's the last cookie
c_end = document.cookie.length;
return unescape (document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCookie(){
username=getCookie('username');
if(username!=null && username!="")
alert('welcome again ' + username+ '!');
else {
username=prompt('please enter your name:',"");
if(username!=null && username!="")
setCookie('username',username,365);
}
}
</script>
</head>
<body onload="checkCookie()">
</body>
</html>
http://jsbin.com/AcanusA/12/edit
There're two errors in your code. One unclosed ")" (in setCookie() function just after last toGMTString();) and one unclosed "}" (in getCookie() function, just after if(c_start!=-1){).
Just take a look in your javascript console to check this errors. Here's the correction:
<html>
<head>
<script>
function setCookie(c_name,value,expiredays){
var exdate = new Date();
exdate.setDate(exdate.getDate()+expiredays);
if(expiredays==null)
document.cookie = c_name + "=" + escape(value) +"";
else
document.cookie = c_name + "=" + escape(value) + ";expires="
+exdate.toGMTString();
}
function getCookie(c_name){
if(document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");
if(c_start!=-1){
c_start=c_start + c_name.length +1;
//index of the value's end
c_end=document.cookie.indexOf(";",c_start);
if(c_end==-1)//if it's the last cookie
c_end = document.cookie.length;
return unescape (document.cookie.substring(c_start,c_end));
}
}
return "";
}
function checkCookie(){
username=getCookie('username');
if(username!=null && username!="")
alert('welcome again ' + username+ '!');
else {
username=prompt('please enter your name:',"");
if(username!=null && username!="")
setCookie('username',username,365);
}
}
</script>
</head>
<body onload="checkCookie()">
za
</body>
</html>
Here's an optimized version of your code:
function getCookie(name) {
var setPos = document.cookie.indexOf(name + '='), stopPos = document.cookie.indexOf(';', setPos);
return !~setPos ? null : document.cookie.substring(
setPos, ~stopPos ? stopPos : undefined).split('=')[1];
}
function setCookie(name, val, days, path) {
var cookie = name + "=" + escape(val) + "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
cookie += ";expires=" + date.toGMTString();
}
cookie += ";" + (path || "path=/");
console.log(cookie);
document.cookie = cookie;
}
var username = getCookie("username");
if (!username) {
setCookie("username", prompt("please enter your name:"), 365);
} else {
alert("welcome again " + username);
}
See test case on jsFiddle.
For some reason this script gets stuck in a redirection loop and wont let you leave "android.html". The url bar shows it is trying to go to "index.html" but it just flashes then stays on "android.html"
var caution = false
function setCookie(name, value, expires, path, domain, secure) {
var curCookie = name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "")
if (!caution || (name + "=" + escape(value)).length <= 4000)
document.cookie = curCookie
else
if (confirm("Cookie exceeds 4KB and will be cut!"))
document.cookie = curCookie
}
function getCookie(name) {
var prefix = name + "="
var cookieStartIndex = document.cookie.indexOf(prefix)
if (cookieStartIndex == -1)
return null
var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
if (cookieEndIndex == -1)
cookieEndIndex = document.cookie.length
return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}
function deleteCookie(name, path, domain) {
if (getCookie(name)) {
document.cookie = name + "=" +
((path) ? "; path=" + path : "") +
((domain) ? "; domain=" + domain : "") +
"; expires=Thu, 01-Jan-70 00:00:01 GMT"
}
}
function fixDate(date) {
var base = new Date(0)
var skew = base.getTime()
if (skew > 0)
date.setTime(date.getTime() - skew)
}
var now = new Date()
fixDate(now)
now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000)
var visits = getCookie("indexVisited")
if (!visits)
window.location.replace("./android.html");
else
window.location.replace("./index.html");
setCookie("indexVisited", visits, now)
Besides missing a whole lot of semicolons, your code tries to set the cookie after it redirects to another page. Javascript stops executing upon a redirect, so your setCookie call never gets executed. Try moving it up before the redirects.
I am setting a cookie, but I don't want it to just work for one directory. How do I make it to where it's read throughout my whole site? I'm pretty new at JavaScript, thanks guys!
<script>
function setCookie(val) {
var d = new Date();
d.setDate(d.getDate() + 300);
document.cookie = "roster_count" + "=" + escape(val) + "; expires=" + d.toGMTString();
delete d;
}
</script>
The the path /:
document.cookie = "roster_count" + "=" + escape(val) + "; expires=" + d.toGMTString() + "; path=/";