2

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

3
  • i haven't heard about any disadvantages Commented Mar 13, 2014 at 16:35
  • 1
    Well firstly you must establish the actual intention of using that script. User having javascript disabled, having a plugin that disables such sudden redirects, SEO.. Commented Mar 13, 2014 at 16:36
  • 1
    @Scorpion please explain how this would affect SEO, thanks Commented Mar 13, 2014 at 16:38

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

Okay, can you please explain deeper how using ob_start() will let you use header whenever you want? thanks.
ob_start() buffers output, meaning any output you have won't cause headers to be sent, meaning you can add new headers at any time.
2

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'" />.

2 Comments

People can disable META refresh too.
Yes, your answer seems like the best solution. :)
1

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.

Comments

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.