Is there a reason my javascript function is being ran without being called?












0















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`









share|improve this question


















  • 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 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
















0















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`









share|improve this question


















  • 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 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














0












0








0








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`









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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 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














  • 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 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








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












1 Answer
1






active

oldest

votes


















1














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/






share|improve this answer

























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


    }
    });














    draft saved

    draft discarded


















    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









    1














    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/






    share|improve this answer






























      1














      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/






      share|improve this answer




























        1












        1








        1







        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/






        share|improve this answer















        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/







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 21:59

























        answered Nov 15 '18 at 21:50









        AnonymousSBAnonymousSB

        2,229221




        2,229221
































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values