简体   繁体   中英

modify url parameters using jQuery without refreshing the page

I searched for hours and I found a script in jquery which modify the url of current page. For example

?profile_id=<%=(rs_resq.Fields.Item("profile_id").Value)%>#inter

But, when I use it the page reloads and I loose all previous cached data in the DOM. I thought the reason behind this happening was the link attribute

rel="external"

so I took it off and then it stopped working.

<script>
$("#inter").live("pageshow", function onPageShow(e,data){

    alert('Page 2 - CID: ' + getParameterByName('profile_id'));
});

function getParameterByName(name) {
    var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
    return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}     
    </script>

NO, You CAN NOT!

A URL is always unique for a particular resource on internet. For example,

www.example.com/resource1.html

is always different from

www.example.com/resource2.html

As soon the url is changed in address bar the browser seeks for the resource where the URL is pointing to, not the same one or anything unknown.

Also, a hash-sign (#) denotes an anchor on the same page, so if you want to change the url to point to an anchor on the same page, like:

www.example.com/resource1.html#anotherColumn

it will just scroll the view to the referred section, not refresh the content.

There are frameworks available, like - AngularJS, KnockoutJS etc. which allows you to access dynamic contents with the same url but different shebang pointers - #! . But again, your page gets refreshed with every change in the url.

Hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM