1

What is the best way to handle Ajax vs. Full page requests with PHP ?

I mean if I click a button that calls Ajax function and loads necessary info into a div and changes the URL.

How do I make that link work if the user copy it and use later to reach the same page.

Hieu Le
  • 7,645
  • 1
  • 30
  • 51
msharpp
  • 349
  • 1
  • 5
  • 20
  • 1
    You look for the hash in the URL and do the necessary stuff. Your best bet is to get a JavaScript MVC framework that handles it for you. – Gerben Jacobs Mar 19 '14 at 09:56

4 Answers4

0

In some of the apps I separate it with a hash. Something like a router and my code goes something like this:

var preRoute = document.URL.split('#');
if(preRoute[1] != undefined) {
  //ajax call
} else {
  //default page
}
tux
  • 1,001
  • 10
  • 14
0

you can try Jquery Adress http://www.asual.com/jquery/address/, demo here http://www.asual.com/jquery/address/samples/state/ or http://vietcode.vn

0

You could check the headers. jQuery for example will add the following key/value to the header: X-Requested-With: XMLHttpRequest for ajax calls. See also this question

Community
  • 1
  • 1
Menno
  • 118
  • 1
  • 7
0

Using: jquery pjax - https://github.com/defunkt/jquery-pjax

you can detect on server side if you get X-PJAX request header

And a great feature of this plugin is the history manipulation and ease of use.

Kszili
  • 304
  • 3
  • 15