ASP.NET postbacks lose the hash in the URL

后端 未结 3 986
再見小時候
再見小時候 2021-02-20 02:14

On an ASP.NET page with a tabstrip, I\'m using the hash code in the URL to keep track of what tab I\'m on (using the BBQ jQuery plugin). For example:

http://mysi         


        
3条回答
  •  野性不改
    2021-02-20 02:59

    I tried to put the code from Willem's answer into a JS function that got called everytime a new tab was activated. This didn't work because it kept appending an additional #hash part to the URL every time I switched tabs.

    My URL ended up looking like http://myurl.example.com/home#tab1#tab2#tab3#tab2 (etc.)

    I modified the code slightly to remove any existing #hash component from the URL in the

    element's action attribute, before appending on the new one. It also uses jQuery to find the element.

    $('.nav-tabs a').on('shown', function (e) {
        // ensure the browser URL properly reflects the active Tab
        window.location.hash = e.target.hash;
    
        // ensure ASP.NET postback comes back to correct tab
        var aspnetForm = $('#aspnetForm')[0];
        if (aspnetForm.action.indexOf('#') >= 0) {
            aspnetForm.action = aspnetForm.action.substr(0, aspnetForm.action.indexOf('#'));
        }
        aspnetForm.action += e.target.hash;
    });
    

    Hope this helps someone!

提交回复
热议问题