schwegler
schwegler

Reputation: 11

JavaScript Redirect if Cookie Present

I'm using javascript and a wildcard to redirect a user to a thank-you page if they have already filled out a form.

<script type="text/javascript">
    if (document.cookie.search(/\bwebform-\S*=/) >= 0)   {
        location.href = "/thanks"; 
    }
</script>

The cookie generated by the Drupal module is webform-62[1234356].

The numbers a randomly generated. The redirect isn't working. Any suggestions?

Upvotes: 1

Views: 845

Answers (2)

Vlad Stratulat
Vlad Stratulat

Reputation: 1296

Use:

document.location.href = "/thanks";

Instead of:

location.href = "/thanks"; 

Upvotes: 0

Florian Margaine
Florian Margaine

Reputation: 60737

if ( /webform-\d/.test( document.cookie ) ) {
    location.href = '/thanks';
}

The regex means: matches webform- plus any number.

Upvotes: 2

Related Questions