Cambiar una variable URL de PHP con Ajax/Javascript

I have made up a webpage on a private server as part of a full website redesign. The page calls up files such as contact.php with AJAX code to show the contents within a DIV layer, so that the actual page is still index.php but looks like a contact page.

What I would like to do is have a function that, when such links to make the AJAX call are clicked, will dynamically change the URL so that if somebody clicks the Contact link the page URL looks like index.php?page=contact (as an example).

Facebook has the sort of system I am looking to usewhen you click on a photo from the news feed and then change to another photo from the same user/gallery. I don't want something that just changes the URL from a hash tag (#), as that just adds complication to the design, whereas one that puts a PHP variable into the URL simply means I can write a PHP code to include the file directly if the variable is set.


preguntado el 25 de agosto de 12 a las 02:08

So you mean like BeatPort where if you start playing a sample and change the page, it continues uninterrupted? Changing the URL is done with JS AFAIK -

1 Respuestas

Have you considered using History.js?

It sounds like you're wanting to essentially use $_GET vars to dictate what's served on the page... kind of. Although it also looks like you're calling in that content via ajax based on this var, so php is mostly irrelevant in this scenario since it would be js parsing the url and deciding what to ajax in. History will do all of this for you. It reads the url as a state and serves as you tell it to. Obviously it's not that simple, but if you read the docs it's pretty clear how to use it.

Respondido 25 ago 12, 02:08

The demo on the Github page is halfway towards what I'm after, but it is using hashes - and I'm wanting it to use the PHP identifier "?", so if somebody clicks the 'about us' link, which currently opens a new DIV and AJAXes an external file into the DIV - but would then also add something like "?about" or "?page=about" trailing on the URL, which then dissappears if the person clicks the link on the page to go back (which hides and clears the new DIV). In addition, the Github example seesm to rely on links being within the <UL> list, which limits what can be done. - nelson

... In addition, it also seems to rely on the state being within the HREF element of the <a> tag, whereas I want the state to be within the NAME element (example: <a name="#/about" href="/es/about.php" onclick="ajaxfunction('include_about.php')"> ) - nelson

History is pretty flexible in how you choose to use it. Since you are deciding to push a new state to history, you can make this happen any way you choose, be it on click, reading the name parameter, or a listener in jquery. It's your call really. You can have it read the url on load and fire off any ajax methods, if clearing a div is a problem. It all depends on how you set up the structure of your site. - kaiqing

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.