Question

First off, I'd like to say that I'm very new to javascript, jquery and/or php. Of the latter I know nothing and the other two aren't far from that.

That being said, I have been scouring the internet for many hours on too many occasions to count for this (admittedly somewhat specific and yet so widely used) thing. It seems to me as though redirecting people to a splash page with a cookie is as well-kept a secret as the recipe for Coca-Cola.

What I want is rather simple, I thought. When the visitor goes to my website, they arrive at the index.html (as per usual). I want to implement a cookie onto this page that applies itself to the visitor. It must check whether the visitor has been to the site before or not. If not, it redirects to splash.html instead. The splash page will be used for the visitor to select the language in which he/she wishes to view the website with links to other HTML pages - this I can make. I have found some snippets here and there, but the javascript seemed too complicated and to always have something missing, somehow, to be able to apply to me and function properly.

Please help!

To recapitulate:

Visitor loads index.html

Cookie acknowledges

if visited before

Do nothing

else

Redirect to splash.html

Seriously, thank you to anyone who can help me with this.

Also, any help as to where to learn Javascript from absolute scratch easily is appreciated.

Was it helpful?

Solution 2

I have found out that this solution works for what I need:

$(function() {
    var COOKIE_NAME = 'splash-page-cookie';
    $go = $.cookie(COOKIE_NAME);
    if ($go == null) {
        $.cookie(COOKIE_NAME, 'test', { path: '/', expires: 183 });
        window.location = "Splash.html"
    }
    else {
    }
});

This script works with the Carhartl Jquery cookie plugin. Thanks for all the help!

OTHER TIPS

There's a good article on working with cookies in Javascript at Mozilla Developer Network: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie

What should help you is Example #3bis:

if (document.cookie.replace(/(?:(?:^|.*;\s*)someCookieName\s*\=\s*([^;]*).*$)|^.*$/, "$1") !== "true") {
    alert("Do something here!");
    document.cookie = "someCookieName=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
}

When you get the value of document.cookie, you get the semicolon-separated list of cookies. The regular expression in the first line finds the value of cookie named someCookieName.

When you set the value of document.cookie, you set or update one cookie (you don't overwrite the whole list of cookies as it may look)

In order to redirect a visitor, you can use document.location property.

The following code should do what you need:

if (document.cookie.replace(/(?:(?:^|.*;\s*)seenSplash\s*\=\s*([^;]*).*$)|^.*$/, "$1") !== "true") {
    document.cookie = "seenSplash=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/";
    document.location = "/splash.html";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top