tags:

views:

475

answers:

2

Hi,

I have an application with URLs like this:

domain.com/category1/category2/ etc.

I see that ajax understands the # and can pass the params to my php script. I am wondering if there is a way in ajax to do the following URL:

domain.com/#category1/category2/

If so, is there a function that I can use in jquery to do this? I have seen jquery bbq but im a big confused how this helps me. It feels like there is an easier way, to just remove the hash and pass the remaining url to my php script, then return the page fragment?

How would I set up my php script to return the main page fragment without the header and footer being refreshed? Do I need to detect that # or javascript has been called and then return the relevant fragment?

i am trying it this way but its not posting the Ajax : hasher parameter.

<script>
hasher = document.location.hash;
hasher = hash.replace(/^.*#/, '');
$.ajax({ type: 'POST', url: url, data: {ajax:hasher}, dataType: 'html' }); 
</script>
<?php
echo $_POST['ajax'];
?>

Am I doing something wrong?

Cheers for any helps

Ke

+1  A: 

You can get the hash value (after #) by getting document.location.hash property (this is standard property, not jQuery) and then pass it to the server-side script by calling $.ajax with some param like {hash:document.location.hash} in data.

Ololo
Not sure i understand this. Is $.ajax part of jquery? Where do i put $.ajax?? what does {hash:document.location.hash} mean and how can i identify that in my php script?
Ke
i can see that this {hash:document.location.hash} is a parameter, but do i post it?
Ke
Yes, $.ajax is a part of jQuerySomething like that:$.ajax({ type: 'POST', url: url, data: {hash:document.location.hash}, dataType: 'json'});
Ololo
This isnt working, i cant work out what im doing wrongI have edited my post above to show what im doing :/
Ke
Do I have to use a click event to do this? Its just not passing the posted parameter when i add #
Ke
A: 

Am I doing something wrong?

Yes.

  1. You are using hash var in second line instead of hasher.
  2. wrong regexp: you're trying to find anything BEFORE the #symbol, but you have just to remove # from the start of the document.location.hash
  3. Take a look into following example

    $('a.submit').click(function(){
        var hasher = document.location.hash;
        hasher = (hasher.length>0)?hasher.substr(1):'';
        $.ajax({
            type: 'POST', 
            url: '/test.php', 
            data: {ajax:hasher}, 
            dataType: 'html',
            success:function(response) {
                $('#ajax').html(response)
            }
        }); 
        return false;
    });
    
Ololo