Which is best window location JS vs header PHP for redirection

0 votes

Which one I should use ? or another ?

using JS : (in <head> tag)

<script>window.location="https://edureka.co";</script>

using PHP : (in <head> tag)

header('Location: https://edureka.co');
end();

Which one I should use ? or another ?

and what about using <meta>?

<meta http-equiv="refresh" content="0;url=https://edureka.co"/> 
Jul 7, 2020 in Java-Script by kartik
• 37,520 points
8,786 views

1 answer to this question.

0 votes

Hello @kartik,

It depends on how and when you want to redirect the user to another page.

If you want to instantly redirect a user to another page without seeing anything on a site in between, you should use the PHP header redirect method.

If you have a Javascript and some action of the user has to result in him entering another page, that is when you should use window.location.

The meta tag refresh is often used on download sites whenever you see these "Your download should start automatically" messages. You can let the user load a page, wait for a certain amount of time, then redirect him (e.g. to a to-be-downloaded file) without Javascript.

Hope it helps!!

Thank You!!

answered Jul 7, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

Which is the best Airbnb clone built with React?

Hello @rentall , For you query you can refer ...READ MORE

answered Oct 20, 2020 in Java-Script by Niroj
• 82,840 points
1,607 views
0 votes
1 answer

Jest SecurityError: localStorage is not available for opaque origins

Hello kartik, You must specify what environment (--env) ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
5,048 views
0 votes
1 answer

What is the difference between node.js and io.js?

Hello, io.js: Node-forward is basically being merged into io.js forked on ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
858 views
0 votes
1 answer

How to access PHP session variables from jQuery function in a .js file?

Hello, You can produce the javascript file via ...READ MORE

answered Apr 29, 2020 in Java-Script by Niroj
• 82,840 points
12,991 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,069 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,016 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,944 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,871 views
0 votes
1 answer

Error:No Access-Control-Allow-Origin header is present on the requested resource node.js

Hello @kartik, My guess is that you're serving ...READ MORE

answered Jun 8, 2020 in Java-Script by Niroj
• 82,840 points
4,686 views
0 votes
1 answer

What is an appropriate content-type header for JavaScript files?

Hello @kartik, JS has two registered MIME types:  The ...READ MORE

answered Jul 7, 2020 in Java-Script by Niroj
• 82,840 points
2,655 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP