Question

So, I have been learning some Javascript, and I came across the:

window.location = "website goes here.."; 

Now I have now learned that this could be used to redirect the user, I remember long ago i wanted so much to know how I can redirect users, without anchor tags and header(location:) since i couldn't use header after headers been sent.

SO I ask you guys, what are the disadvantages of using this Javascript to deal with redirecting my users after a if statement for example, or are there simply none.

Thanks

Was it helpful?

Solution

If the user has JavaScript disabled, your redirect won't work.

Personally, I start my PHP code with ob_start(), then I can use header wherever I want :p

OTHER TIPS

A possible disadvantage is that the User can disable Javascript. You can either use PHP, or you can use <meta http-equiv="refresh" content="0; URL='URL Here'" />.

I would say the primary problem with using javascript to redirect a user is that the redirect is not guaranteed. So for sensitive redirects that are important one should use header() or something that you know will work. If the redirect is simply an added bonus for your user then using javascript will work just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top