Adding a parameter to the URL with JavaScript

后端 未结 30 2270
刺人心
刺人心 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 07:59

    Easiest solution, works if you have already a tag or not, and removes it automatically so it wont keep adding equal tags, have fun

    function changeURL(tag)
    {
    if(window.location.href.indexOf("?") > -1) {
        if(window.location.href.indexOf("&"+tag) > -1){
    
            var url = window.location.href.replace("&"+tag,"")+"&"+tag;
        }
        else
        {
            var url = window.location.href+"&"+tag;
        }
    }else{
        if(window.location.href.indexOf("?"+tag) > -1){
    
            var url = window.location.href.replace("?"+tag,"")+"?"+tag;
        }
        else
        {
            var url = window.location.href+"?"+tag;
        }
    }
      window.location = url;
    }
    

    THEN

    changeURL("i=updated");
    

提交回复
热议问题