0

I have this php-jquery code

<?php
//a php code determines value of the variable $url
echo "<script>
$('#main').html('Loading...'); 
$('#main').load('".$url."');
</script>";
?>

But, when the path indicated in $url doesn't exist, the content of #main remains stuck to "Loading..." and it doesn't display a 404 error. How can I fix this?

  • Your url is not valid (not found) – Kristiyan Mar 29 '14 at 16:40
  • Maybe my explanation wasn't clear enough, I apologize for that. $url is determined dynamically, and could be (not always) non-existent. In this case, I'd like to display a custom message. –  Mar 29 '14 at 16:43
  • Ajax `error: function(jqXHR, exception) {if (jqXHR.status == 404) { alert("Requested page not found. [404]");//show error}}` – BlackPearl Mar 29 '14 at 16:48
  • You can check if url not exist and then show "error page". http://stackoverflow.com/questions/2280394/how-can-i-check-if-a-url-exists-via-php – Kristiyan Mar 29 '14 at 16:48
  • Thank you both for your answers. –  Mar 29 '14 at 16:51

1 Answers1

0

The reason why "Loading" is stuck there is because .load() is an ajax call and although returns a 400 response, does not actually reload the page.

What you can do is make use of jQuery load() callback function like

$("...").load("yourURL", function (responseText, textStatus, req) {
   if (textStatus == "error") {
      //do something here, like redirect the page for example
   }
});
Rey Libutan
  • 5,226
  • 9
  • 42
  • 73