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

6
  • 1
    By the source of this, it doesn't look like a jQuery plugin. Commented Aug 3, 2017 at 17:09
  • source is only to show cookie keys. I am logged in and i dont get redirected to /premium. Commented Aug 3, 2017 at 17:10
  • Are you using this plugin? Commented Aug 3, 2017 at 17:10
  • 2
    Did you forget quotes around Sentry_firstName, or is that variable declared somewhere that you're not showing? Commented Aug 3, 2017 at 17:12
  • And there is a missing parenthesis to close the .on method. Commented Aug 3, 2017 at 17:14

3 Answers 3

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>
Sign up to request clarification or add additional context in comments.

23 Comments

There is a { missing at the end of the condition and a ) to close the .on().
Ah... And also, the attr() arguments are inversed... Should be .attr( attributeName, value ).
: $ is not defined on line 1 of script
@Balleleif: It means jQuery lib isn't loaded.
Wrap your code with $(document).ready(function(){ and }); instead of $( window ).on( "load", function() {
|
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.

Comments

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>

1 Comment

The settings in main html stil overtakes. even with jquery, block and script on top of html.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.