admin管理员组

文章数量:1426097

I'm attempting to create a redirect based on a cookie existence. So when a user connects to my website jonathanstevens for the first time, they are redirected to jonathanstevens/landing

Code parts:

<script src=".2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

Global.js

function create_cookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime( date.getTime() + (days*24*60*60*1000) );
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie(name) {
    var nameEQ = name + "=";
    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, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
<script src=".1.1/jquery.min.js"></script>

I'm attempting to create a redirect based on a cookie existence. So when a user connects to my website jonathanstevens for the first time, they are redirected to jonathanstevens/landing

Code parts:

<script src="https://code.jquery./jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>

Global.js

function create_cookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime( date.getTime() + (days*24*60*60*1000) );
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function get_cookie(name) {
    var nameEQ = name + "=";
    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, c.length);
        }
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Index.html

<!-- redirect to landing page -->
<script>
  // Redirect to landing page if 'form_submitted' cookie does not exist
  if (get_cookie('secondvisit') === 'undefined') {
    window.location.href = "landing.html";
  }
</script>

landing.html

<!-- Adds second Visit cookie -->
<script>
	jQuery(document).ready(function($) {
    // Create cookie so that the user is no longer redirected
    create_cookie('secondvisit', 'true', 30);
	});
</script>

The expected result was it to check for a cookie, then forward me to my landing page if it wasn't defined. Any ideas on what I've done wrong?

Share Improve this question edited Dec 7, 2017 at 19:21 Muhammad Omer Aslam 23.8k9 gold badges46 silver badges69 bronze badges asked Dec 7, 2017 at 1:18 Jonathan StevensJonathan Stevens 771 gold badge1 silver badge16 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 2

You should pare with null instead of undefined as you are returning null from the function get_cookie

Index.html

<!-- redirect to landing page -->
<script>
  // Redirect to landing page if 'form_submitted' cookie does not exist
  if (get_cookie('secondvisit') === null) {
    window.location.href = "landing.html";
  }
</script>

Apart from this you should use this library really good one an easy to work with see below

Create a cookie, valid across the entire site:

Cookies.set('name', 'value');

Create a cookie that expires 7 days from now, valid across the entire site:

Cookies.set('name', 'value', { expires: 7 });

Create an expiring cookie, valid to the path of the current page:

Cookies.set('name', 'value', { expires: 7, path: '' });

Read cookie:

Cookies.get('name'); // => 'value'
Cookies.get('nothing'); // => undefined

Read all visible cookies:

Cookies.get(); // => { name: 'value' }

Delete cookie:

Cookies.remove('name');

Delete a cookie valid to the path of the current page:

Cookies.set('name', 'value', { path: '' });
Cookies.remove('name'); // fail!
Cookies.remove('name', { path: '' }); // removed! 

EDIT

A converted version of your code using js.cookie library will look like following.

(Note: i have tested this code and it works correctly, make sure you are including the library correctly and there are no errors on the console.)

Index.html

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr/npm/js-cookie@2/src/js.cookie.min.js"></script>
<!-- redirect to landing page -->
<script>
    $(document).ready(function () {
        if (typeof Cookies.get('secondvisit') === 'undefined') {
            window.location.href = "landing.html";
        }
    })
    // Redirect to landing page if 'form_submitted' cookie does not exist
</script>

landing.html

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr/npm/js-cookie@2/src/js.cookie.min.js"></script>
<!-- Adds second Visit cookie -->
<script>
    jQuery(document).ready(function () {
        // Create cookie so that the user is no longer redirected
        var a = Cookies.set('secondvisit', 'true', {
            expires: 7
        });
    });
</script>

本文标签: jqueryCookie based Redirection using JavascriptStack Overflow