Is there a reason my javascript function is being ran without being called?
I am running a script to verify a users age. The function being used to check for the cookie works the first time a user goes to a page, and redirects to the home page where the age verification lives if they do not have the requested cookie.
Once this happens, the cookie is actually being created on the redirect at which point the user can go to any subpage of the site. Could anyone point out why this could be happening?
//Setting The Value Of The Cookie
var this_cookies_value = "_addMeToTheBrowser_";
//Checks page on load to see if this_cookies_value already exists
function checkForOurCookiesValue() {
var allTheCookies = document.cookie;
var _this_Host_Name_ = '"' + window.location.hostname + '"';
var _this_Path_Name = window.location.pathname;
console.log(allTheCookies);
if(allTheCookies.includes(this_cookies_value) || _this_Path_Name == "/") {
console.log("WORKING");
} else {
window.location.replace(_this_Host_Name_);
};
}
checkForOurCookiesValue();
//If cookie does not exist, this script will run once users age is verified correctly
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
return true;
}
//Creating the cookie
jQuery(".the_btn").on("click", createCookie("_my_domain_", this_cookies_value, 1));`enter code here`
javascript jquery cookies
add a comment |
I am running a script to verify a users age. The function being used to check for the cookie works the first time a user goes to a page, and redirects to the home page where the age verification lives if they do not have the requested cookie.
Once this happens, the cookie is actually being created on the redirect at which point the user can go to any subpage of the site. Could anyone point out why this could be happening?
//Setting The Value Of The Cookie
var this_cookies_value = "_addMeToTheBrowser_";
//Checks page on load to see if this_cookies_value already exists
function checkForOurCookiesValue() {
var allTheCookies = document.cookie;
var _this_Host_Name_ = '"' + window.location.hostname + '"';
var _this_Path_Name = window.location.pathname;
console.log(allTheCookies);
if(allTheCookies.includes(this_cookies_value) || _this_Path_Name == "/") {
console.log("WORKING");
} else {
window.location.replace(_this_Host_Name_);
};
}
checkForOurCookiesValue();
//If cookie does not exist, this script will run once users age is verified correctly
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
return true;
}
//Creating the cookie
jQuery(".the_btn").on("click", createCookie("_my_domain_", this_cookies_value, 1));`enter code here`
javascript jquery cookies
6
You call the function in the last line of your code
– tkausl
Nov 15 '18 at 21:37
3
You probably meant to writejQuery(".the_btn").on("click", function(event) { createCookie("_my_domain_", this_cookies_value, 1); });
– Bergi
Nov 15 '18 at 21:41
1
on('click', createCookie(...))
is not the same ason('click', function(){ createCookie(...)})
– charlietfl
Nov 15 '18 at 21:41
Ahh, yes, thanks you guys for this. Issues is solved now. @bergi
– JaySnel
Nov 15 '18 at 21:47
add a comment |
I am running a script to verify a users age. The function being used to check for the cookie works the first time a user goes to a page, and redirects to the home page where the age verification lives if they do not have the requested cookie.
Once this happens, the cookie is actually being created on the redirect at which point the user can go to any subpage of the site. Could anyone point out why this could be happening?
//Setting The Value Of The Cookie
var this_cookies_value = "_addMeToTheBrowser_";
//Checks page on load to see if this_cookies_value already exists
function checkForOurCookiesValue() {
var allTheCookies = document.cookie;
var _this_Host_Name_ = '"' + window.location.hostname + '"';
var _this_Path_Name = window.location.pathname;
console.log(allTheCookies);
if(allTheCookies.includes(this_cookies_value) || _this_Path_Name == "/") {
console.log("WORKING");
} else {
window.location.replace(_this_Host_Name_);
};
}
checkForOurCookiesValue();
//If cookie does not exist, this script will run once users age is verified correctly
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
return true;
}
//Creating the cookie
jQuery(".the_btn").on("click", createCookie("_my_domain_", this_cookies_value, 1));`enter code here`
javascript jquery cookies
I am running a script to verify a users age. The function being used to check for the cookie works the first time a user goes to a page, and redirects to the home page where the age verification lives if they do not have the requested cookie.
Once this happens, the cookie is actually being created on the redirect at which point the user can go to any subpage of the site. Could anyone point out why this could be happening?
//Setting The Value Of The Cookie
var this_cookies_value = "_addMeToTheBrowser_";
//Checks page on load to see if this_cookies_value already exists
function checkForOurCookiesValue() {
var allTheCookies = document.cookie;
var _this_Host_Name_ = '"' + window.location.hostname + '"';
var _this_Path_Name = window.location.pathname;
console.log(allTheCookies);
if(allTheCookies.includes(this_cookies_value) || _this_Path_Name == "/") {
console.log("WORKING");
} else {
window.location.replace(_this_Host_Name_);
};
}
checkForOurCookiesValue();
//If cookie does not exist, this script will run once users age is verified correctly
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
return true;
}
//Creating the cookie
jQuery(".the_btn").on("click", createCookie("_my_domain_", this_cookies_value, 1));`enter code here`
javascript jquery cookies
javascript jquery cookies
asked Nov 15 '18 at 21:35
JaySnelJaySnel
256
256
6
You call the function in the last line of your code
– tkausl
Nov 15 '18 at 21:37
3
You probably meant to writejQuery(".the_btn").on("click", function(event) { createCookie("_my_domain_", this_cookies_value, 1); });
– Bergi
Nov 15 '18 at 21:41
1
on('click', createCookie(...))
is not the same ason('click', function(){ createCookie(...)})
– charlietfl
Nov 15 '18 at 21:41
Ahh, yes, thanks you guys for this. Issues is solved now. @bergi
– JaySnel
Nov 15 '18 at 21:47
add a comment |
6
You call the function in the last line of your code
– tkausl
Nov 15 '18 at 21:37
3
You probably meant to writejQuery(".the_btn").on("click", function(event) { createCookie("_my_domain_", this_cookies_value, 1); });
– Bergi
Nov 15 '18 at 21:41
1
on('click', createCookie(...))
is not the same ason('click', function(){ createCookie(...)})
– charlietfl
Nov 15 '18 at 21:41
Ahh, yes, thanks you guys for this. Issues is solved now. @bergi
– JaySnel
Nov 15 '18 at 21:47
6
6
You call the function in the last line of your code
– tkausl
Nov 15 '18 at 21:37
You call the function in the last line of your code
– tkausl
Nov 15 '18 at 21:37
3
3
You probably meant to write
jQuery(".the_btn").on("click", function(event) { createCookie("_my_domain_", this_cookies_value, 1); });
– Bergi
Nov 15 '18 at 21:41
You probably meant to write
jQuery(".the_btn").on("click", function(event) { createCookie("_my_domain_", this_cookies_value, 1); });
– Bergi
Nov 15 '18 at 21:41
1
1
on('click', createCookie(...))
is not the same as on('click', function(){ createCookie(...)})
– charlietfl
Nov 15 '18 at 21:41
on('click', createCookie(...))
is not the same as on('click', function(){ createCookie(...)})
– charlietfl
Nov 15 '18 at 21:41
Ahh, yes, thanks you guys for this. Issues is solved now. @bergi
– JaySnel
Nov 15 '18 at 21:47
Ahh, yes, thanks you guys for this. Issues is solved now. @bergi
– JaySnel
Nov 15 '18 at 21:47
add a comment |
1 Answer
1
active
oldest
votes
You need to make your function an anonymous (nameless) function in your last line.
Your code
jQuery(".the_btn")
.on(
"click",
createCookie("_my_domain_", this_cookies_value, 1)
);
Correct code
jQuery(".the_btn")
.on(
"click",
function() {
createCookie("_my_domain_", this_cookies_value, 1)
}
);
When your code is read by the browser it sees createCookie()
as a function call it needs to run right away. If you put it inside an anonymous function, then it won't be called, rather another function will be created to be called later by the click
event.
Considering you're using jQuery, I assume you need full browser support, and IE doesn't support "fat arrow" functions yet; however, if you don't need IE support, you can use the following code:
jQuery(".the_btn")
.on(
"click",
() => createCookie("_my_domain_", this_cookies_value, 1)
);
Demo
https://jsfiddle.net/AnonymousSB/Lyn58bax/2/
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%2f53328236%2fis-there-a-reason-my-javascript-function-is-being-ran-without-being-called%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
You need to make your function an anonymous (nameless) function in your last line.
Your code
jQuery(".the_btn")
.on(
"click",
createCookie("_my_domain_", this_cookies_value, 1)
);
Correct code
jQuery(".the_btn")
.on(
"click",
function() {
createCookie("_my_domain_", this_cookies_value, 1)
}
);
When your code is read by the browser it sees createCookie()
as a function call it needs to run right away. If you put it inside an anonymous function, then it won't be called, rather another function will be created to be called later by the click
event.
Considering you're using jQuery, I assume you need full browser support, and IE doesn't support "fat arrow" functions yet; however, if you don't need IE support, you can use the following code:
jQuery(".the_btn")
.on(
"click",
() => createCookie("_my_domain_", this_cookies_value, 1)
);
Demo
https://jsfiddle.net/AnonymousSB/Lyn58bax/2/
add a comment |
You need to make your function an anonymous (nameless) function in your last line.
Your code
jQuery(".the_btn")
.on(
"click",
createCookie("_my_domain_", this_cookies_value, 1)
);
Correct code
jQuery(".the_btn")
.on(
"click",
function() {
createCookie("_my_domain_", this_cookies_value, 1)
}
);
When your code is read by the browser it sees createCookie()
as a function call it needs to run right away. If you put it inside an anonymous function, then it won't be called, rather another function will be created to be called later by the click
event.
Considering you're using jQuery, I assume you need full browser support, and IE doesn't support "fat arrow" functions yet; however, if you don't need IE support, you can use the following code:
jQuery(".the_btn")
.on(
"click",
() => createCookie("_my_domain_", this_cookies_value, 1)
);
Demo
https://jsfiddle.net/AnonymousSB/Lyn58bax/2/
add a comment |
You need to make your function an anonymous (nameless) function in your last line.
Your code
jQuery(".the_btn")
.on(
"click",
createCookie("_my_domain_", this_cookies_value, 1)
);
Correct code
jQuery(".the_btn")
.on(
"click",
function() {
createCookie("_my_domain_", this_cookies_value, 1)
}
);
When your code is read by the browser it sees createCookie()
as a function call it needs to run right away. If you put it inside an anonymous function, then it won't be called, rather another function will be created to be called later by the click
event.
Considering you're using jQuery, I assume you need full browser support, and IE doesn't support "fat arrow" functions yet; however, if you don't need IE support, you can use the following code:
jQuery(".the_btn")
.on(
"click",
() => createCookie("_my_domain_", this_cookies_value, 1)
);
Demo
https://jsfiddle.net/AnonymousSB/Lyn58bax/2/
You need to make your function an anonymous (nameless) function in your last line.
Your code
jQuery(".the_btn")
.on(
"click",
createCookie("_my_domain_", this_cookies_value, 1)
);
Correct code
jQuery(".the_btn")
.on(
"click",
function() {
createCookie("_my_domain_", this_cookies_value, 1)
}
);
When your code is read by the browser it sees createCookie()
as a function call it needs to run right away. If you put it inside an anonymous function, then it won't be called, rather another function will be created to be called later by the click
event.
Considering you're using jQuery, I assume you need full browser support, and IE doesn't support "fat arrow" functions yet; however, if you don't need IE support, you can use the following code:
jQuery(".the_btn")
.on(
"click",
() => createCookie("_my_domain_", this_cookies_value, 1)
);
Demo
https://jsfiddle.net/AnonymousSB/Lyn58bax/2/
edited Nov 15 '18 at 21:59
answered Nov 15 '18 at 21:50
AnonymousSBAnonymousSB
2,229221
2,229221
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%2f53328236%2fis-there-a-reason-my-javascript-function-is-being-ran-without-being-called%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
6
You call the function in the last line of your code
– tkausl
Nov 15 '18 at 21:37
3
You probably meant to write
jQuery(".the_btn").on("click", function(event) { createCookie("_my_domain_", this_cookies_value, 1); });
– Bergi
Nov 15 '18 at 21:41
1
on('click', createCookie(...))
is not the same ason('click', function(){ createCookie(...)})
– charlietfl
Nov 15 '18 at 21:41
Ahh, yes, thanks you guys for this. Issues is solved now. @bergi
– JaySnel
Nov 15 '18 at 21:47