Best way of redirecting user to a new page with Java/JavaScript?

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
Should I use this Java code from the server to redirect the user to a new page
Code:
response.sendRedirect(URLString);
Or should I just send the URL as a string with PrintWriter so JavaScript can use
Code:
window.location = URLString;

Im going back over some of my early code and AJAXifying it which required me to send the URL to JavaScript rather than use Java sendRedirect() method. I like consistency so if I can have the whole website work the same way for requests that would be great. Theres no downfalls to just sending the URL string right?
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
The rule I use is, whichever requires the fewest hits to the server.

It sounds to me like both your ajax fetch new url and your send them to a redirect will require the same numbers of servers hits.

One problem with window.location = URLString; is that you have to depend on the browser for how it handles that statement. I can remember some browser doesn't do anything but requires window.location.href to be used.

I can't remember the browser but people discuss it here:
https://stackoverflow.com/questions/2383401/javascript-setting-location-href-versus-location
 

sbpromania

Senior member
Mar 3, 2015
265
1
16
www.sbp-romania.com
Hi Maximilian,

It depends on what causes the redirect action to be triggered: is it after a server-side processing (Java code), or after a
processing that takes place on the client-side (JavaScript)?

So for example, if the user clicks on a button, which causes a postback (for example for saving an entry in the database),
which in turn requires a redirect, using response.sendRedirect will actually send an HTTP Meta Refresh, which is faster than
loading the page for performing a window.location call.

The opposite would be having a button that saves a cookie for example (via JavaScript), then redirects to a new page using
window.location. There would be no point (and not optimal either) to do a postback (full or partial), in order to use
response.sendRedirect.

Also, keep in mind that you cannot use permanent redirects (301) by using window.location.

So in short, you could unify the code to use only 1 of the methods you indicated, but it may not be optimal
(performance-wise).
 

Maximilian

Lifer
Feb 8, 2004
12,604
15
81
Cheers guys :thumbsup:

Yeah I think that using a mix of the two methods seems to be the best idea. If a new page is going to be loaded then I guess theres no real point in using AJAX. Ive used an AJAX call to load the main page the user sees when they first login, this allows me to leverage AJAX to give feedback to them if their login failed for whatever reason.

@sbpromania Yeah it does seem slower to use window.location, I never noticed that until you mentioned it but my main page that uses AJAX is slightly slower to load than the others that use response.sendRedirect(). Anyway the other pages I wanted to "standardize" have been left as they are. Ill just use the right tool for the job even if I have to fight my urge to standardize things :)