-1

I've been unable to figure out the right way to implement this mainly because of syntax issues as I'm fairly new to all of this.

I'd like to check if a user has been to a certain page (page2). I've got this script:

<script>
    $.cookie('visitPage2', true, { expires: 365 });
</script>

My first question is:
Will that immediately run once the page is loaded? That is the entirety of JS on that page. Back on my homepage I'm running this script to change an image from displaying a lock to displaying the actual image instead. (but I know my syntax isn't right):

<script>
    if (!$.cookie('visitobj1')){
    $( ".swap1" ).toggleClass( "lock unlock" );
    };
</script>  

I had previously checked to see if my toggleClass was by just testing it with a click instead of uploading to the server to test cookies and it worked perfectly:

<script>
    $( ".swap" ).click(function() {
    $( ".swap" ).toggleClass( "lock unlock" );
    };
</script>

Any help is much appreciated! Thanks!

(Just a quick summary in case I have been too wordy. I want to check if a user has been to Page2. If they have I want the homepage to display a new image instead of the usual lock image, which is changed using toggleClass)

Cookie.js documentation is here: https://github.com/js-cookie/js-cookie

  • 2
    You are missing the closing bracket (`}`) for the `if` statement. If you look in the error console you will see a syntax error message. – Guffa Jun 06 '15 at 13:58
  • Fixed in the post! let me know if that looks okay? Still unsure why it's not running. Thanks for your help! – ThatOneGuy Jun 06 '15 at 15:24
  • 2
    Now you have an extra parenthesis (`)`) after the closing bracket. **Open the error console** and make sure you don't have any syntax errors before trying anything else. – JJJ Jun 06 '15 at 15:24
  • 1
    Fixed! And I've checked the console as well and it's looking like theres a different problem: "Uncaught TypeError: $.cookie is not a function" So I'm thinking that my html files aren't seeing the cookie.js file which is strange, because it's linked the exact same way as all my other js. . I've rearranged the position of where this line is in my code to no success. Thanks again for everyone's help! – ThatOneGuy Jun 06 '15 at 15:45
  • @ThatOneGuy, you are probably using the version 2.0.0 beta of js-cookie. In the version 2 the `$` was removed and you should use `Cookies` instead. Take a look here: https://github.com/js-cookie/js-cookie/releases/tag/v2.0.0-beta.1 – Fagner Brack Jun 06 '15 at 18:23

1 Answers1

0

My first question is: Will that immediately run once the page is loaded?

It will not run once the page is loaded assuming you consider "page loaded" as "all HTML is loaded". The browser reads the HTML from top to bottom, when he reaches that script tag, then the script will run. So, it will not run once the page is loaded, it will run while the page is loaded.

Note: You mentioned the js-cookie repository. In js-cookie version 2.0.0 the cookie creation API was changed from $.cookie( name, value, options ) to Cookies.set( name, value, attributes ).
So you in your case you need to write it as as Cookies.set( 'visitPage2', 'true', { expires: 365 } ).

Fagner Brack
  • 2,365
  • 4
  • 33
  • 69