Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Cookie Path JavaScript

I want the code below to set the path to root, I understand I have to set / as the path value however this is not my code and I am not familiar with Javascript!

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" : "");
}

I have tried editing the code as below but have been unsuccessful.

 setCookie(name, value, expires, path, domain, secure){

document.cookie= name + "=" + escape(value) +
((expires) ? "; expires=" + expires.toGMTString() : "") +
((path) ? "; path="/") +
((domain) ? "; domain=" + domain : "") +
((secure) ? "; secure" : "");
}
like image 379
Harry Avatar asked Jan 24 '14 09:01

Harry


People also ask

How do I set cookies to all path?

In your Java server, you should call cookie. setPath("/") before adding it to response. Such cookie will match all request URIs.

Can I set-cookie for another path?

You can't access cookies from a different path - otherwise it would be a security hole.

Can you set a cookie in JavaScript?

Change a Cookie with JavaScript With JavaScript, you can change a cookie the same way as you create it: document. cookie = "username=John Smith; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/"; The old cookie is overwritten.

What is path in set-cookie?

Set a cookie path The path parameter specifies a document location for the cookie, so it's assigned to a specific path, and sent to the server only if the path matches the current document location, or a parent: document.


1 Answers

change to this

function setCookie(name, value, expires, path, domain, secure){
    document.cookie= name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ("; path=/") +       //you having wrong quote here
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}
like image 158
leo108 Avatar answered Sep 23 '22 02:09

leo108