Adding a parameter to the URL with JavaScript -


In a web application that uses AJAX calls, I need to submit a request, but at the end a URL: For example:

The original URL:

The resulting URL:

< P> & amp; Enabled = true

is looking for a Javascript function which parses URLs by looking at each parameter, adds new parameter or if one already exists then value Updates.

A basic implementation that you will need to optimize, will look something like this:

Enter function param (key, value) {key = encodeuri (key); Value = encoder (value); Var kvp = document.location.search.substr (1) .split ('& amp;'); Var i = kvp.length; Var x; Whereas (i--) {x = kvp [i] .split ('='); If (x [0] == key) {x [1] = value; Kvp [i] = x.join ('='); break; }} If (i and lt; 0) {kvp [kvp.length] = [key, value] .join ('=');} // will reload this page, it is far better until complete document Will happen. place. Search = kvp.join ('& amp;'); }

It is almost twice as fast as a regex or search based solution, but it depends entirely on the length of the string of the string and the index of any match

= EncodurIComponent (key); Value = encoder value (value); Var s = document.location.search; Var kvp = key + "=" + value; Var r = New RegExp ("(& amp; | \\?)" + Key + "= [^ \ & amp;;] *"); S = s.replace (r, "$ 1" + kvp); If (! RegExp. $ 1) {s + = (s.length> 0? & Amp; ':'? ') + Kvp;}; // Again, do whatever you do here. place. Search = s; }


Comments