Adding a parameter to the URL with JavaScript

后端 未结 30 2576
刺人心
刺人心 2020-11-22 07:33

In a web application that makes use of AJAX calls, I need to submit a request but add a parameter to the end of the URL, for example:

Original URL:

30条回答
  •  迷失自我
    2020-11-22 08:03

    With the new achievements in JS here is how one can add query param to the URL:

    var protocol = window.location.protocol,
        host = '//' + window.location.host,
        path = window.location.pathname,
        query = window.location.search;
    
    var newUrl = protocol + host + path + query + (query ? '&' : '?') + 'param=1';
    
    window.history.pushState({path:newUrl}, '' , newUrl);
    

    Also see this possibility Moziila URLSearchParams.append()

提交回复
热议问题