1

Trying to make a window.onload "if logged in redirect to /premium" script.

<script>
$( window ).on( "load", function() {
  if (!!$.cookie(Sentry_firstName)) {
    $(location).attr('/premium',url);
  }
}
</script>

This does not work when pasted into .HTML any ideas? Dont need backend. can have it all in HTML. And have i read the cookie key right? Cookie source HERE

Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64
Balleleif
  • 11
  • 4

3 Answers3

2

To test if a cookie exist you can use javascript:

if (document.cookie.indexOf('Sentry_firstName') > -1 ) {

your code become :

<script>
$(document).ready(function(){
  if (document.cookie.indexOf('Sentry_firstName') > -1 ) {
    window.location = '/premium';
  }
});
</script>
jeremy-denis
  • 6,368
  • 3
  • 18
  • 35
0

Thanks all for the replies. This worked:

 <script>
      $(document).ready(function(){
        if (document.cookie.indexOf('COOKIEKEY') > -1 ) {
        window.location = 'URL';
            }
            }); 
</script>

However. Page loads and reset settings before re-entering location "URL". Back end modification to already made .JS file and how to run it in HTML would be apreciated to.

Balleleif
  • 11
  • 4
0

Starting from the code that works (in your own answer), to hide the page before the decision to redirect or not is made, add this css:

<style>
body{
  display:none !important;
}
</style>

And add an else to your script, to display the page if no redirect.

<script>
  $(document).ready(function(){
    if (document.cookie.indexOf('COOKIEKEY') > -1 ) {
      window.location = 'URL';
    }else{
      $("body").css({"display":"block"});
    }
  }); 
</script>
Louys Patrice Bessette
  • 33,375
  • 6
  • 36
  • 64