Query if Location = X redirect

Hello Experts,

I have a neat JQuery login function, which is working nicely

  $("#SignIn").submit(function(e){
      e.preventDefault();
    $.ajax({
		type: "POST",
        url: "/actions/login.asp",
        data:$(this).serialize(),
		success:function(result){
          $('#SignInError1').hide();
          $('#SignInError2').hide();
         if(result==1){
            // do something fail
			var email = $('#signinusername').val();	
	      $('#SignInError1').html('<p>Sorry there is no account registered with the username ' + email + '. Please <a class="fancybox" href="#Register" rel="nofollow">register</a> an account to continue</p>');
          $('#SignInError1').fadeIn('slow');
          }
		  else if (result==2){
            // do something fail
			var email = $('#signinusername').val();
	      $('#SignInError2').html('<p>The password you have provided is not correct. Please try again or <a class="fancybox" href="#PasswordRequestfrm" rel="nofollow">Reset your password</a></p>');
          $('#SignInError2').fadeIn('slow');
          }
		  else {
            // do something success
     		 $.fancybox.close();
			 location.reload();          
			 }
  	  }});
  });

Open in new window


However this function can be called from a Password Reset page (password reset.asp).. if this is the case, I don't want to just reload the page, I would prefer to redirect the user to an account page..

Any ideas how to do this?

Thanks
garethtnashAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
The best way to do this is return a JSON structure to the function that contains information on what you want it to do

For instance

...
success: function(resp) {
    if (typeof resp.redirect !== 'undefined') {
       window.location = resp.redirect
    }
    else {
       // Normal processing here
    }
}

Open in new window

garethtnashAuthor Commented:
Any chance you could show me please? the password reset page is
/passwordreset/passreset.asp
Thanks
Julian HansenCommented:
First up I would simplify your logon page I would only have 1 error message. By providing two you are giving unauthorised users clues on names and passwords. If you give them a password incorrect message but not a username - they know they have guessed a username so can focus on the password.

For the PassReset - perhaps something like this. In this instance the .asp script returns an empty string ('') if there was an error otherwise it returns the URL to redirect to.

$("#Reset").submit(function(e){
  e.preventDefault();
  $.ajax({
    type: "POST",
    url: "/actions/passreset.asp",
    data:$(this).serialize(),
    success:function(result){
      $('#PasswordError').hide();
      if(result==''){
        // do something fail
        $('#PasswordError').html('<p>Sorry the passwords do not match or do not meet security requirements</p>');
        $('#PasswordError').fadeIn('slow');
      }
      else {
        window.location = result;
      }
    }
  });
});

Open in new window

Another alternative - using a hard coded redirect
$("#Reset").submit(function(e){
  e.preventDefault();
  $.ajax({
    type: "POST",
    url: "/actions/passreset.asp",
    data:$(this).serialize(),
    success:function(result){
      $('#PasswordError').hide();
      if (result == 1) {
        window.location = 'your redirect url here';
        return;
      }
      if(result==-1){
        // do something fail
        $('#PasswordError').html('<p>Sorry the passwords do not match</p>');
      }
      else if (result == -2)
        $('#PasswordError').html('<p>Password does not meet security requirements. You need at least 1 Capital letter, 1 lowercase letter, a digit and a non-alphanumeric character</p>');
      }
      $('#PasswordError').fadeIn('slow');
    }
  });
});

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

garethtnashAuthor Commented:
Hey Julian,

Thanks for the examples.

Sorry I don't think that I explained my question properly. the Form SignIn can be called from any page, when completed the JQuery below processes the form

//////////////////////////      Login     /////////////////////////////////

  $("#SignIn").submit(function(e){
      e.preventDefault();
    $.ajax({
		type: "POST",
        url: "/actions/login.asp",
        data:$(this).serialize(),
		success:function(result){
          $('#SignInError1').hide();
          $('#SignInError2').hide();
         if(result==1){
            // do something fail
			var email = $('#signinusername').val();	
	      $('#SignInError1').html('<p>Sorry there is no account registered with the username ' + email + '. Please <a class="fancybox" href="#Register" rel="nofollow">register</a> an account to continue</p>');
          $('#SignInError1').fadeIn('slow');
          }
		  else if (result==2){
            // do something fail
			var email = $('#signinusername').val();
	      $('#SignInError2').html('<p>The password you have provided is not correct. Please try again or <a class="fancybox" href="#PasswordRequestfrm" rel="nofollow">Reset your password</a></p>');
          $('#SignInError2').fadeIn('slow');
          }
		  else {
            // do something success
     		 $.fancybox.close();
			 location.reload();          
			 }
  	  }});
  });

Open in new window


I take on your point about not giving to many error message options and will adjust.

If the script runs successfully the page calling the SignIn form reloads and UserAuth cookies are set.

Which is fine for most pages.

The issue I have though is that the page '/passwordreset/passreset.asp' might also call the SignIn form, in which case on success I don't want to just refresh '/passwordreset/passreset.asp' as this would be confusing, I would instead want to redirect to an alternative page say '/account/settings.asp'

Hope that makes sense.

Thanks for your help.
Julian HansenCommented:
then just check before you do the reload if it is the password reset page i.e

if (location.pathname == '/passwordreset/passreset.asp') {
  window.location = '/account/settings.asp';
}
else {
   location.reload();
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
garethtnashAuthor Commented:
Hi Julian,

Thanks - so currently the JQuery is

  $("#SignIn").submit(function(e){
      e.preventDefault();
    $.ajax({
		type: "POST",
        url: "/actions/login.asp",
        data:$(this).serialize(),
		success:function(result){
          $('#SignInError1').hide();
          $('#SignInError2').hide();
         if(result==1){
            // do something fail
			var email = $('#signinusername').val();	
	      $('#SignInError1').html('<p>Sorry there is no account registered with the username ' + email + '. Please <a class="fancybox" href="#Register" rel="nofollow">register</a> an account to continue</p>');
          $('#SignInError1').fadeIn('slow');
          }
		  else if (result==2){
            // do something fail
			var email = $('#signinusername').val();
	      $('#SignInError2').html('<p>The password you have provided is not correct. Please try again or <a class="fancybox" href="#PasswordRequestfrm" rel="nofollow">Reset your password</a></p>');
          $('#SignInError2').fadeIn('slow');
          }
		  else {
            // do something success
     		 $.fancybox.close();
				if (location.pathname == '/passwordreset/passreset.asp') {
				  window.location = '/account/settings.asp';
				}
				else {
				   location.reload();
				}       
			 }
  	  }});
  });

Open in new window


I've not updated the error response yet.

Just tested this from a page with the URL

'/passwordreset/passreset.asp?ST0xJlU9Z2FyZXRodGFubmF0dG5hc2hAZ21haWwuY29t#'

The login happened, but the page just reloaded?

Any thoughts?

Thanks
Julian HansenCommented:
And if you do this
if (location.pathname == '/passwordreset/passreset.asp') {
  window.location = '/account/settings.asp';
}
else {
  alert(location.pathname);
  location.reload();
}

Open in new window

What appears in the alert dialog?
garethtnashAuthor Commented:
I've got it working now, thank you
garethtnashAuthor Commented:
Thank you
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.