gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)
ajax set cookie
- User
ajax set cookie
- Assistant
To set a cookie using AJAX (Asynchronous JavaScript and XML), you can use the
document.cookieproperty in JavaScript.Here's an example of how you can do it:
function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + value + expires + "; path=/"; } // Example usage setCookie("myCookieName", "myCookieValue", 7);In this example, the
setCookiefunction takes three parameters: thenameof the cookie, thevalueto be stored, and the number ofdaysthe cookie should remain active. It sets the cookie by assigning the desired value todocument.cookie.Note that cookies are stored as strings and multiple cookies are separated by semicolons. Also, the
pathparameter indocument.cookieis set to "/" to make the cookie accessible across the entire website.You can call this
setCookiefunction within your AJAX request, passing the necessary parameters.