1

which one is better php or jquery ?

 header('Location: http://myhost.com/mypage.php');
     or
printf('<script type="text/javascript">location.href = " http://myhost.com/mypage.php";</script>');

and when do we use one over the other? Thanks . Is it users interest or do they have any conditions to follow. Kindly let me know, what is the basic need of header and how does it really work.

niko
  • 9,285
  • 27
  • 84
  • 131

6 Answers6

3

If it's just a matter of redirect, I would use PHP (no possibility that user can stop this), as if user has disabled JavaScript, the redirection will fail.

If you want a simpler one, you can do it with simple HTML like

<meta http-equiv="refresh" content="0;url=NEW PAGE URL" /> 
<!-- Redirect Immediately-->

Or after say 30 seconds

<meta http-equiv="refresh" content="30;url=NEW PAGE URL" /> 
<!-- Redirects after 30 secs -->

Note: If you are using header() for redirection, make sure you don't output any HTML or you don't have any white space or you'll get a warning which says headers already sent

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • white space ? what does it mean . can you explain me more clearly. – niko Jun 15 '13 at 12:10
  • is there anything wrong on using meta tag for this purpose? – niko Jun 15 '13 at 12:11
  • @niko http://stackoverflow.com/questions/8028957/headers-already-sent-by-php and no, no issue if you use meta tag, after all it redirects too and it will work even if JS is disabled but I would prefer PHP. – Mr. Alien Jun 15 '13 at 12:12
2

If you are redirecting in PHP, printing a line of javascript to redirect doesn't make much sense, as PHP will redirect before any content is sent to the browser, while javascript will redirect after the page has loaded.

Use PHP.

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • oh so will it load the entire page I guessed it would redirect as soon as it finds location url so only after the loading of page jquery redirects? – niko Jun 15 '13 at 11:43
  • It will redirect as soon as the script tag is parsed by the browser, which would be after the headers, cookies, and at least some of the content is sent to the browser, while PHP will redirect on the serverside, much sooner than any javascript redirect. – adeneo Jun 15 '13 at 11:50
0

php is better as it does not relief on JavaScript been on/off, but it also depends on what you are doing. if its a jquery submit form and you need to redirect then jquery otherwise php.

amigura
  • 541
  • 3
  • 6
0

PHP one is normally used until unless it is required because of some condition that you need to use javascript. Also php redirect would work even if browser javascript is disabled. Javascript redirect is mostly used on some kind of event and condition like

$('#clickMe').click(function(){

   if(somecondition){
   header.location = 'http://....';
   }

})
nickhar
  • 19,981
  • 12
  • 60
  • 73
tornados
  • 86
  • 5
0

In the PHP scenario, the header() does in fact send information back to the browser (client side) and the browser then makes the new request to the new URL. So it is the same client->server->client->server->client. The difference is that with the PHP model, you only send headers back on the first round not HTML. This is more efficient. So, yes, the PHP model is a better solution but you technically have the same number of communication loops. To learn more, Google HTML headers and header communication.

0

These two approaches can be used for solving different problems, ex:

If you want programmatically controll application flow from your UI use javascript redirect, you can render html and depending on UI action redirect customer.

If you want just redirect customer as fast as possible backend solution makes more sense, it won't load any html just sends to browser new header with new Location and browser as soon as it gets it redirects user.

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26