tags:

views:

62

answers:

2

I am using AJAX/JSON combination in Zend Framework. Whenever I click any link in my application, AJAX request is called and content is loaded successfully in DIVs But the address bar is unchanged. How to change address bar according to current active action.

Here is My Working Code:


When I use http://practice.dev/ my index.phtml file is loaded.

<a href='http://practice.dev/index/one' class='ajax'>One</a>
<a href='http://practice.dev/index/two' class='ajax'>Two</a>

<div id="content">content comes here</div>

one.phtml:

$jsonArray = array( 'content' => 'One' );
echo Zend_Json::encode( $jsonArray );

two.phtml:

$jsonArray = array( 'content' => 'Two' );
echo Zend_Json::encode( $jsonArray );

JS Code

jQuery(document).ready(function(){
    jQuery('.ajax').click(function(event) {
        event.preventDefault();
        jQuery.getJSON(this.href, function(snippets) {
            for(var id in snippets) {
                jQuery('#' + id).html(snippets[id]);
            }
        });
    });
});

When I click link one then string 'One' is loaded in content DIV but address bar is still http://practice.dev/. It should be http://practice.dev/index/one

When I click link two then string 'Two' is loaded in content DIV but address bar is still http://practice.dev/. It should be http://practice.dev/index/two

How is it possible ?

Thanks

+2  A: 

This:

event.preventDefault();

is what is preventing the address bar from updating.

I don't believe there is a way to change the URL (document.location.href) itself without causing a browser redirect. Even changing the fragment (document.location.hash) will cause a "scroll" (depending on whether or not the browser window is small enough to scroll).

EDIT: Well, it'll cause a scroll to the respective anchor only if such an anchor exists in your page, as Pekka notes. So I guess you could try that.

If you want your page to be indexable by Google despite the Ajax and the Ajax-modified URLs, start your fragments with ! to allow Google to crawl your Ajax pages.

BoltClock
Changing the fragment is how these solutions usually work (I think they simply don't place an anchor in the document, so no scrolling will occur)
Pekka
When I remove `event.preventDefault();` then URL is changed to `http://practice.dev/index/one` But it does not load json data into DIVs and just print `{"content":"One"}` on the screen. Any alternative ?
NAVEED
@NAVEED: exactly, you should not be removing that call as it's necessary for your Ajax to work properly. Based on Pekka's comment, you can set the hash fragment of your URL instead.
BoltClock
@BoltClock: Yes I saw in some application that whenever I click AJAX link it changes the URL something like this: `http://practice.dev/#index/one`. But I don't know how to implement **hash fragment** logic.
NAVEED
@NAVEED: do all the content-loading and server-side stuff with your Ajax implementation. Then set `document.location.hash` to `index/one`, etc.
BoltClock
@BoltClock: I changed my links like this: `<a href='/index/one' class='ajax'>One</a>`. Then I added this line in my js code: `document.location.hash=$(this).attr("href");`. Now it is changing URL to `http://practice.dev/#/index/one` and also loading DIVs properly. Thanks.
NAVEED
I didn't know Google did that, useful!
jakenoble
@jakenoble: [I was frankly quite baffled myself when I saw `#!` in Facebook's URLs](http://stackoverflow.com/questions/3009380/whats-the-shebang-in-facebook-urls-for) :)
BoltClock
+2  A: 

Remove event.preventDefault();, this is stopping the default behavior for the link occurring. The default behavior being - going to the URL in the href attribute.

Edit based on more info from OP

You need to make your javascript respond to the hash tag.

Instead of having a URL schema just like this http://practice.dev/index/one it needs to have a hash tag version too, that javascript can handle like this http://practice.dev/index#one. This means that if someone visits the page http://practice.dev/index#two your javascript can know that it needs to go and get the second page, even though Zend will have got the first page already.

In your javascript you can get the hash tag using

var hash = location.hash;

And set it using

location.hash = "two";

There is more about this here

So leave your links the same like this http://practice.dev/index/one but append a hash tag to the URL when the user clicks a link, you CAN add hash tags the address bar. Change your javascript to load a certain page number if there is a hash tag present when the page loads.

jakenoble
But this is not really what he wants (which he will soon discover when he removes the preventDefault)
Pekka
When I remove `event.preventDefault();` then URL is changed to `http://practice.dev/index/one` But it does not load json data into DIVs and just print `{"content":"One"}` on the screen. Any alternative ?
NAVEED
Do you want the div populating? Or do yo want the URL in the address bar to change? The whole point of AJAX is that it does is asynchronously, in the background. Are you sure your using it correctly?
jakenoble
I want DIV populating as well as URL change is address bar. Now div populating is working perfectly with my code mentioned in question but problem is unchanged URL in address. I listen about hash fragment in URL to solve this problem but dont know how to implement.
NAVEED
I have updated my answer for you
jakenoble
+1 Its good. Thanks.
NAVEED