HTML js and making cookies
I need to make a basic cookie through js in my html. the js has to be in a separate file named javascript.js and in the html I need to call the function to make and store a cookie. I have
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
before my body to create the source in the file. I have a button trying to call a function getCookie so in my body ive got
<button onclick="js.getCookie">COOKIE</button>
there should be a popup asking for my name but im not sure why its not working.
the js file
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
its a basic thing it took from https://www.w3schools.com/js/js_cookies.asp because im trying to understand cookies
javascript html
add a comment |
I need to make a basic cookie through js in my html. the js has to be in a separate file named javascript.js and in the html I need to call the function to make and store a cookie. I have
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
before my body to create the source in the file. I have a button trying to call a function getCookie so in my body ive got
<button onclick="js.getCookie">COOKIE</button>
there should be a popup asking for my name but im not sure why its not working.
the js file
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
its a basic thing it took from https://www.w3schools.com/js/js_cookies.asp because im trying to understand cookies
javascript html
1
Welcome to Stackoverflow.onclickproperty should point to a function. I don't thinkjs.getCookieis a valid javascript function. Can you show the content of the js file?
– Ahmad
Nov 15 '18 at 6:28
Are you getting any console errors? Hit F12 in your browser of choice to view the Developer Tools, which includes the console. If you have console errors, have you tried to fix them and what are they? Most importantly what is injavascript.js? Please provide the relevant code from the javascript file. While you're in Browser Tools, use the Network tab to make sure the javascript file is loaded (look for 404 errors)
– Jon P
Nov 15 '18 at 6:33
Here is a good resource for getting and setting cookies: developer.mozilla.org/en-US/docs/Web/API/Document/cookie
– Jon P
Nov 15 '18 at 6:37
Simply, you are calling the wrong function. CallcheckCookienotgetCookieand dropjs.
– Jon P
Nov 15 '18 at 6:41
add a comment |
I need to make a basic cookie through js in my html. the js has to be in a separate file named javascript.js and in the html I need to call the function to make and store a cookie. I have
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
before my body to create the source in the file. I have a button trying to call a function getCookie so in my body ive got
<button onclick="js.getCookie">COOKIE</button>
there should be a popup asking for my name but im not sure why its not working.
the js file
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
its a basic thing it took from https://www.w3schools.com/js/js_cookies.asp because im trying to understand cookies
javascript html
I need to make a basic cookie through js in my html. the js has to be in a separate file named javascript.js and in the html I need to call the function to make and store a cookie. I have
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
before my body to create the source in the file. I have a button trying to call a function getCookie so in my body ive got
<button onclick="js.getCookie">COOKIE</button>
there should be a popup asking for my name but im not sure why its not working.
the js file
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}
its a basic thing it took from https://www.w3schools.com/js/js_cookies.asp because im trying to understand cookies
javascript html
javascript html
edited Nov 15 '18 at 6:37
Zach Lyness SonicEX
asked Nov 15 '18 at 6:25
Zach Lyness SonicEXZach Lyness SonicEX
112
112
1
Welcome to Stackoverflow.onclickproperty should point to a function. I don't thinkjs.getCookieis a valid javascript function. Can you show the content of the js file?
– Ahmad
Nov 15 '18 at 6:28
Are you getting any console errors? Hit F12 in your browser of choice to view the Developer Tools, which includes the console. If you have console errors, have you tried to fix them and what are they? Most importantly what is injavascript.js? Please provide the relevant code from the javascript file. While you're in Browser Tools, use the Network tab to make sure the javascript file is loaded (look for 404 errors)
– Jon P
Nov 15 '18 at 6:33
Here is a good resource for getting and setting cookies: developer.mozilla.org/en-US/docs/Web/API/Document/cookie
– Jon P
Nov 15 '18 at 6:37
Simply, you are calling the wrong function. CallcheckCookienotgetCookieand dropjs.
– Jon P
Nov 15 '18 at 6:41
add a comment |
1
Welcome to Stackoverflow.onclickproperty should point to a function. I don't thinkjs.getCookieis a valid javascript function. Can you show the content of the js file?
– Ahmad
Nov 15 '18 at 6:28
Are you getting any console errors? Hit F12 in your browser of choice to view the Developer Tools, which includes the console. If you have console errors, have you tried to fix them and what are they? Most importantly what is injavascript.js? Please provide the relevant code from the javascript file. While you're in Browser Tools, use the Network tab to make sure the javascript file is loaded (look for 404 errors)
– Jon P
Nov 15 '18 at 6:33
Here is a good resource for getting and setting cookies: developer.mozilla.org/en-US/docs/Web/API/Document/cookie
– Jon P
Nov 15 '18 at 6:37
Simply, you are calling the wrong function. CallcheckCookienotgetCookieand dropjs.
– Jon P
Nov 15 '18 at 6:41
1
1
Welcome to Stackoverflow.
onclick property should point to a function. I don't think js.getCookie is a valid javascript function. Can you show the content of the js file?– Ahmad
Nov 15 '18 at 6:28
Welcome to Stackoverflow.
onclick property should point to a function. I don't think js.getCookie is a valid javascript function. Can you show the content of the js file?– Ahmad
Nov 15 '18 at 6:28
Are you getting any console errors? Hit F12 in your browser of choice to view the Developer Tools, which includes the console. If you have console errors, have you tried to fix them and what are they? Most importantly what is in
javascript.js? Please provide the relevant code from the javascript file. While you're in Browser Tools, use the Network tab to make sure the javascript file is loaded (look for 404 errors)– Jon P
Nov 15 '18 at 6:33
Are you getting any console errors? Hit F12 in your browser of choice to view the Developer Tools, which includes the console. If you have console errors, have you tried to fix them and what are they? Most importantly what is in
javascript.js? Please provide the relevant code from the javascript file. While you're in Browser Tools, use the Network tab to make sure the javascript file is loaded (look for 404 errors)– Jon P
Nov 15 '18 at 6:33
Here is a good resource for getting and setting cookies: developer.mozilla.org/en-US/docs/Web/API/Document/cookie
– Jon P
Nov 15 '18 at 6:37
Here is a good resource for getting and setting cookies: developer.mozilla.org/en-US/docs/Web/API/Document/cookie
– Jon P
Nov 15 '18 at 6:37
Simply, you are calling the wrong function. Call
checkCookie not getCookie and drop js.– Jon P
Nov 15 '18 at 6:41
Simply, you are calling the wrong function. Call
checkCookie not getCookie and drop js.– Jon P
Nov 15 '18 at 6:41
add a comment |
1 Answer
1
active
oldest
votes
When you use an inline js function in your html you also need to have parentheses behind it. I've pasted your code into a snippet and made the function call work.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313586%2fhtml-js-and-making-cookies%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you use an inline js function in your html you also need to have parentheses behind it. I've pasted your code into a snippet and made the function call work.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>add a comment |
When you use an inline js function in your html you also need to have parentheses behind it. I've pasted your code into a snippet and made the function call work.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>add a comment |
When you use an inline js function in your html you also need to have parentheses behind it. I've pasted your code into a snippet and made the function call work.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>When you use an inline js function in your html you also need to have parentheses behind it. I've pasted your code into a snippet and made the function call work.
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
alert("Welcome again " + user);
} else {
user = prompt("Please enter your name:", "");
if (user != "" && user != null) {
setCookie("username", user, 365);
}
}
}<body>
<button onclick="checkCookie();">COOKIE</button>
</body>answered Nov 15 '18 at 6:57
JasonBJasonB
4,28021021
4,28021021
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313586%2fhtml-js-and-making-cookies%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to Stackoverflow.
onclickproperty should point to a function. I don't thinkjs.getCookieis a valid javascript function. Can you show the content of the js file?– Ahmad
Nov 15 '18 at 6:28
Are you getting any console errors? Hit F12 in your browser of choice to view the Developer Tools, which includes the console. If you have console errors, have you tried to fix them and what are they? Most importantly what is in
javascript.js? Please provide the relevant code from the javascript file. While you're in Browser Tools, use the Network tab to make sure the javascript file is loaded (look for 404 errors)– Jon P
Nov 15 '18 at 6:33
Here is a good resource for getting and setting cookies: developer.mozilla.org/en-US/docs/Web/API/Document/cookie
– Jon P
Nov 15 '18 at 6:37
Simply, you are calling the wrong function. Call
checkCookienotgetCookieand dropjs.– Jon P
Nov 15 '18 at 6:41