“JavaScript mengatur parameter kueri di URL” Kode Jawaban

cara mengubah parameter kueri URL di JavaScript

// Construct URLSearchParams object instance from current URL querystring.
var queryParams = new URLSearchParams(window.location.search);
 
// Set new or modify existing parameter value. 
queryParams.set("myParam", "myValue");
 
// Replace current querystring with the new one.
history.replaceState(null, null, "?"+queryParams.toString());

// OR do a push to history
tory.pushState(null, null, "?"+queryParams.toString());
Cute Chinchilla

JavaScript mengatur parameter kueri

function updateQueryStringParameter(uri, key, value) {
  var re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
  var separator = uri.indexOf('?') !== -1 ? "&" : "?";
  if (uri.match(re)) {
    return uri.replace(re, '$1' + key + "=" + value + '$2');
  }
  else {
    return uri + separator + key + "=" + value;
  }
}
//You can reload the url like so
 var newUrl=updateQueryStringParameter(window.location.href,"some_param","replaceValue");
 window.history.pushState("", "Page Title Here", newUrl);
Grepper

Buat URL dengan parameter kueri JavaScript

var query = new URLSearchParams();
query.append("KEY", "VALUE");
var url = "http://site.com/page?" + query.toString();
Masoud Fesahat

JavaScript mengatur parameter kueri di URL

function updateURLParameter(url, param, paramVal)
{
    var TheAnchor = null;
    var newAdditionalURL = "";
    var tempArray = url.split("?");
    var baseURL = tempArray[0];
    var additionalURL = tempArray[1];
    var temp = "";

    if (additionalURL) 
    {
        var tmpAnchor = additionalURL.split("#");
        var TheParams = tmpAnchor[0];
            TheAnchor = tmpAnchor[1];
        if(TheAnchor)
            additionalURL = TheParams;

        tempArray = additionalURL.split("&");

        for (var i=0; i<tempArray.length; i++)
        {
            if(tempArray[i].split('=')[0] != param)
            {
                newAdditionalURL += temp + tempArray[i];
                temp = "&";
            }
        }        
    }
    else
    {
        var tmpAnchor = baseURL.split("#");
        var TheParams = tmpAnchor[0];
            TheAnchor  = tmpAnchor[1];

        if(TheParams)
            baseURL = TheParams;
    }

    if(TheAnchor)
        paramVal += "#" + TheAnchor;

    var rows_txt = temp + "" + param + "=" + paramVal;
    return baseURL + "?" + newAdditionalURL + rows_txt;
}
Amtk 07

Jawaban yang mirip dengan “JavaScript mengatur parameter kueri di URL”

Pertanyaan yang mirip dengan “JavaScript mengatur parameter kueri di URL”

Lebih banyak jawaban terkait untuk “JavaScript mengatur parameter kueri di URL” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya