jquery - Javascript: window.location.search does not update? - Stack Overflow

I need to update search (query) parameters in URL if user enters those in search panel (on a page). I&#

I need to update search (query) parameters in URL if user enters those in search panel (on a page). I'm trying this:

$( document ).ready(function() {
    if ($('.o_website_license_search_panel').length) {
        $('.o_website_license_search_panel .o_search_submit').click(function () {
            var search = $.deparam(window.location.search.substring(1));
            console.log('before update')
            console.log(window.location.search)
            search.license_key = $(".o_website_license_search_panel input[name='license_key']").val();
            console.log('new obj ', search, $.param(search))
            window.location.search = $.param(search);
            console.log('after update')
            console.log(window.location.search)
        });
    }
});

And I get this output:

before update
web.assets_frontend.js:1254 ?license_state=cancel
web.assets_frontend.js:1255 new obj  {license_state: "cancel", license_key: "test2"} license_state=cancel&license_key=test2
web.assets_frontend.js:1256 after update
web.assets_frontend.js:1257 ?license_state=cancel

As you can see window.location.search stays the same. Is there something I miss, or it is intended this way?..

I need to update search (query) parameters in URL if user enters those in search panel (on a page). I'm trying this:

$( document ).ready(function() {
    if ($('.o_website_license_search_panel').length) {
        $('.o_website_license_search_panel .o_search_submit').click(function () {
            var search = $.deparam(window.location.search.substring(1));
            console.log('before update')
            console.log(window.location.search)
            search.license_key = $(".o_website_license_search_panel input[name='license_key']").val();
            console.log('new obj ', search, $.param(search))
            window.location.search = $.param(search);
            console.log('after update')
            console.log(window.location.search)
        });
    }
});

And I get this output:

before update
web.assets_frontend.js:1254 ?license_state=cancel
web.assets_frontend.js:1255 new obj  {license_state: "cancel", license_key: "test2"} license_state=cancel&license_key=test2
web.assets_frontend.js:1256 after update
web.assets_frontend.js:1257 ?license_state=cancel

As you can see window.location.search stays the same. Is there something I miss, or it is intended this way?..

Share Improve this question asked Jan 13, 2018 at 12:01 AndriusAndrius 21.2k47 gold badges154 silver badges264 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

Setting search (or any of the other properties on location other than hash) causes a reload of the page. The property will continue to have its previous value until that happens, which is why you see the previous value in your logging statements. After the old value is logged, unless code on your page is preventing it from happening, the page will be reloaded with the new query string, at which point location.search would reveal the new string.

More: window.location's properties on MDN

You can use the history api provided by javascript also to help out, but to also let you know it won't refresh the page but change the browser url in the url pane, Cleverly done you can achieve what you want

Visit this link for more details

https://developer.mozilla/en-US/docs/Web/API/History_API

Note

Sometimes people use this method for ajax purpose, thereby enabling them to store history data and also for bookmarking

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744198554a4562771.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信