Tony
Tony

Reputation: 12695

Modify the URL wihout reloading the page

I know that there were similar topics, but I still didn't get the working solution. I want to send the $.post method to the server and before it I want to modify the URL (to allow the users to share the URLs by each other) but without reloading the page. How to do it ?

Edit: In other words, I want to make the $.post method to filter the data displayed on the page. But also I want to modify the URL that user can copy it.

Upvotes: 0

Views: 162

Answers (3)

Sid
Sid

Reputation: 7631

Maybe this is what you want?

<body>
    ...
    <div id='urltocopy'></div>
    ...
</body>

function doPost() {
    $('#urltocopy').html('<a href="http://newurltocopy">Please copy</a>');

    $.ajax({
            type: 'POST',
            url: urlStr,
            dataType: 'html',
            success: function(data) {

            },
            data: {},

        });
}

Upvotes: 0

gen_Eric
gen_Eric

Reputation: 227200

If your users are using a modern browser (pretty much anything that's not IE), you can use the new HTML5 history methods. Specifically, history.replaceState.

replaceState() will change the URL of the page without re-loading it. You can use it like this:

$.post('/some/url/', {some: data}, function(d){
    // Do whatever with the returned data...

    // Change page URL
    history.replaceState({}, '', '/some/url/');
});

Or you can change the URL before loading the data:

// Change page URL
history.replaceState({}, '', '/some/url/');
// load page data
$.post('/some/url/', {some: data}, function(d){
    // Do whatever with the returned data...
});

For an example of replaceState, go here: https://github.com/features/projects, and click on the 4 tabs. Watch the URL change, but nothing's added to the history :-)

Upvotes: 2

Armin
Armin

Reputation: 15938

This is not possible. The only part in the url you can modify without a reload is the part after the # (hash a.k.a. fragment). The whole rest before would initiate the load of the new (or old) url.

You can change it (with reload) with the following javascript:

location.href = 'http://www.whatever.com';

Upvotes: 0

Related Questions