I saw this go back code in a website and I am trying to use it on my website. I am having trouble in understanding how the toURl and refUrl are being generated in it. Please guide about their structure. Thanks
var backtriggered = false;
setTimeout('goBack()',timer);
function goBack() {
if(backtriggered) return false;
backtriggered = true;
toURL=getWelcomeCookie('toURL');
refURL = getWelcomeCookie('refURL');
var reg = /(.*)\/sites\/(.*)\/(\d{4})\/(\d{2})\/(\d{2})\/(.*)\/?/;
if(reg.test(toURL) == false){
document.cookie="toURL"+ "=" +escape(toURL)+";path=/; domain=.mycsnippets.com; expires=Thu, 01-Jan-1900 00:00:01 GMT";
document.cookie="refURL"+ "=" +escape(toURL)+";path=/; domain=.mycsnippets.com; expires=Thu, 01-Jan-1900 00:00:01 GMT";
}
location.href=toURL;
}
document.cookie="toURL"+ "=" +escape(toURL)+";path=/; domain=.mycsnippets.com; expires=Thu, 01-Jan-1900 00:00:01 GMT";
This line puts a value in the brower's cookie store. For more information I think you should read this first: http://en.wikipedia.org/wiki/HTTP_cookie
There are two ways to set cookies: through server response, and through javascript.
What this code does is set cookies using javascript which say where the visitor has been and where he is going before setting the url to the target url (toURL). This way next page when it is called it knows to go back to the url which was set in the cookies as the previous url.
Related
I would like to execute the following JavaScript to set a browser cookie:
document.cookie = "name=value;path='/'"
This works fine in Firefox, Chrome, and Safari. It does not work in IE, however. Removing the path part or unquoting '/' seems to set the cookie correctly in IE. I'm not an expert on the cookie spec. All of the guides online seem to quote the path. Is it required or optional to quote the path?
According to W3Schools correct syntax is following:
With a path parameter, you can tell the browser what path the cookie
belongs to. By default, the cookie belongs to the current page.
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
http://www.w3schools.com/js/js_cookies.asp
Yes it is safe. it is the right way to do it when using plain old javascript.
document.cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
or via Jquery plugin jquery.cookie
$.cookie('name', 'value', { expires: 7, path: '/' });
//or
$.cookie('name', 'value', { path: '/' });
I have been searching for reading cookies for last several hours and posted questions in this site but still no luck.
All I need to do is set cookie on one page and read cookie on other page. I have tried escape and unescape but no result.
Here is my code on first page where I am setting cookie
document.cookie = 'province=' +window.escape($(elem).text()) +'; expires=Fri, 3 Dec 2014 20:47:11 UTC; path=/';
And here I am reading it
function re() {
var a = get_cookie("province");
alert(a);
window.location = "lp.aspx?"+a;
}
function get_cookie(cookie_name) {
var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');
if (results)
return (window.unescape(results[1]));
else
return null;
}
I have tried all the answers on stackoverflow but still looking for solution.
Again I need to set cookie on one page and read it on other page.
Try using the following
var testcookieValue = $.cookie("testcookie"); // read
$.cookie("testcookie", 1); // write
Hope it helps.
On an enrollment page using forms on a Drupal module, I'm having trouble loading a cookie onto a series of pages. The purpose of the cookie is to have it load onto a multistep form, and then delete it when the form's done. On the next page, there is supposed to be a script that detects if the cookie still exists, but if it does it redirects the user back to the form.
The code I have for the form is here...
document.cookie = "visited=yes; path=/enrolled/";
$("#enroll-now-fifth-step #edit-return").click(function() {
document.cookie = "visited=yes; expires=Thu, 01 Jan 1970 00:00:01 GMT";
});
});
And the code for the page that detects the cookie and redirects the page back to the form if it exists is here...
document.cookie = "visited=yes; path=/enrolled/";
if (document.cookie.indexOf("visited") >= 0) {
location.href = "/enrolled/step-2";
} else {
document.cookie = "visited=yes; expires=Thu, 01 Jan 1970 00:00:01 GMT";
}
I've been trying constantly for days with no luck. Any suggestions on what to do?
I need to delete a cookie and then do a redirect. However the cookie doesn't get deleted until the redirect is processed. The problem is that if the cookie still exists at the time the redirect is executed, the redirect is intercepted and sent to a page other than the one intended. (weird, I know; long story)
Is it possible to trick the browser and force the deletion of the cookie before the redirect?
jQuery('div#panelD').click(function(){
document.cookie = 'sharedsession=; expires=Thu, 01 Jan 1970 00:00:01 GMT; Domain=.example.com; path=/';
window.location.href = "www.example.com/x";
});
I discovered if I perform an AJAX call for anything, it qualifies as a refresh for the purposed of deleting cookies.
var fakeAjax = new XMLHttpRequest();
var anything = fakeAjax.responseText;
fakeAjax.open("GET","ajax_info.txt",false); // file doesn't actually exist
fakeAjax.send();
Note the "false" in the Open line. Asynchronous has to be set to false (or some other delay) to allow time for the new info to come back and the cookie to be deleted.
UPDATE: IE doesn't like requesting responseText from a file that doesn't exist so just remove that line completely. Other browsers seem to be fine with or without it.
I'm using this code to save cookies:
function saveCookie(name,value) {
var date = new Date();
date.setTime(date.getTime()+(60*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
document.cookie = name+"="+value+expires+"; path=/";
}
My problem is that it saves the cookie with domain "example.com" and I want to write them to ".example.com" so I can also read them from subdomains. This is easy to do with PHP but I don't know how to do it with javascript. How can I add a dot before the domain when I save the cookie?
You already have path in there, domain is specified in the same way.
To permit reading from other sub-domains, try:
'; path=/; domain=.'+window.location.host;