JS replace address bar parameter values

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="color: #ff0000;">first, the page introduces JS code snippet (the whole copy and paste can be):</span></p></p><p><p></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>query=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {"use strict";<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>b=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>b=[],c,d,e,f;<span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a== "undefined" | | a===<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>|| a=== "")<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>B;a.indexof ("?") ===0&& (a=a.substring (1)), d=a.tostring (). split (/[&;] /);<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(c=0;c<d.length;c++) e=d[c],f=e.split ("="), B.push ([f[0],f[1]]);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>B},c=b (a), d=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(){<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>A= "", b,d;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(b=0;b<c.length;b++) d=c[b],a.length>0&& (a+= "&"), a+=d.join ("=");<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>a.length>0? "?" +a:a},e=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {a=decodeuricomponent (a), a=a.replace ("+", "");<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>a},f=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>b,d;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(d=0;d<c.length;d++) {b=c[d];<span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(e (a) ===e (b[0]))<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>b[1]}},g=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>b=[],d,f;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(d=0;d<c.length;d++) f=c[d],e (a) ===e (f[0]) &&b.push (f[1]);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>B},h=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a, B) {<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>d=[],f,g,h,i;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(f=0;f<c.length;f++) g=c[f],h=e (g[0]) ===e (a), i=e (g[1]) ===e (b), (arguments.length===1&&!h| | Arguments.length===2&&!h&&!i) &&d.push (g); c=d;<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},i=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a,b,d) {arguments.length===3&&d!==-1? (d=math.min (d,c.length), c.splice (d,0,[a,b])): arguments.length>0&&c.push ([a, b]);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},j=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a,b,d) {<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>f=-1,g,j;<span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(arguments.length===3) {<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(g=0;g<c.length;g++) {j=c[g];<span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(e (j[0]) ===e (a) &&decodeuricomponent (j[1]) ===e (D)) {f=g;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span>}}h (a,d). AddParam (a,b,f)}<span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span>{<span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(g=0;g<c.length;g++) {j=c[g];<span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span>(e (j[0]) ===e (a)) {f=g;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> break</span>}}h (a), i (a,b,f)}<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>};<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>{getparamvalue:f,getparamvalues:g,deleteparam:h,addparam:i,replaceparam:j,tostring:d}},uri=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {"use strict";<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>B=!1,c=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>c={strict:/^ (?:( [^:\/?#]+):)? (?:\ /\/((?:( ([^:@]*) (?::([^:@]*))?) [email protected])? ([^:\/?#]*) (?::(\d*))?) ((((?:[^?#\/]*\/)*)([^?#]*))(?:\? ([^#]*))? (?:#(.*))?) /,loose:/^ (?:(?! [^:@]+:[^:@\/]*@)([^:\/?#.] +):)? (?:\ /\/)? ((?:( ([^:@]*) (?::([^:@]*))?) [email protected])? ([^:\/?#]*) (?::(\d*))?) (((\/(?:[^?#](?! [^?#\/]*\. [^?#\/.] +(?:[?#]|$)))*\/?)? ([^?#\/]*)) (?:\? ([^#]*))? (?:#(.*))?) /},d=["source", "protocol", "authority", "userInfo", "user", "password", "host", "port", "relative", "path", "directory" , "file", "query", "anchor"],e={name: "querykey", parser:/(?: ^| &) ([^&=]*) =? ([^&]*)/g},f=c[b? " Strict ":" loose "].exec (a), g={},h=14;<span style="color: #0000ff;"><span style="color: #0000ff;"></span> while</span>(h--) g[d[h]]=f[h]| | ""; G[e.name]={},g[d[12]].replace (e.parser,<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a,b,c) {b&& (g[e.name][b]=c)});<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>G},d=c (a| | ""), e=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Query (d.query), f=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (d.protocol=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>d.protocol},g=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>, h=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (g=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>g===<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>? d.source.indexof ("//")!==-1:g},i=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (d.userinfo=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>d.userinfo},j=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (d.host=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>d.host},k=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (d.port=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>D.port},l=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (d.path=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>d.path},m=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (e=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Query (a));<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>e},n=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">typeof</span></span>a!= "undefined" && (d.anchor=a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>d.anchor},o=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {f (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},p=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {h (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},q=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {i (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},r=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {j (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},s=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {k (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},t=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {l (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},u=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {m (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},v=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {n (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},w=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>M (). GetParamValue (a)},x=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>M (). getparamvalues (a)},y=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a, B) {arguments.length===2?m (). deleteparam (a, b): m (). deleteparam (a);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},z=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a,b,c) {arguments.length===3?m (). addparam (a,b,c): m (). addparam (a, b);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},a=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a,b,c) {arguments.length===3?m (). replaceparam (a,b,c): m (). replaceparam (a, b);<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> this</span>},b=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(){<span style="color: #0000ff;"><span style="color: #0000ff;">var</span></span>A= "", b=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(a) {<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>a!==<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span>&&a!== ""};b (f ())? (a+=f (), F (). indexOf (":")!==f () .length-1&& (a+= ":"), a+= "//"): h () &&b (j ()) && (a+= "//"), b (i ( )) &&b (j ()) && (a+=i (), i (). indexOf ("@")!==i () .length-1&& (a+= "@"), b (j ()) && (a+=j () , b (k ()) && (a+= ":" +k ())), b (l ())? a+=l (): b (j ()) && (b (m (). toString ()) | | b (n ())) && (a+= "/"), b (m (). tostring ()) && (m (). tostring (). indexOf ("?")! ==0&& (a+= "?"), a+=m (). toString ()), b (n ()) && (n (). indexOf ("#")!==0&& (a+= "#"), a+=n ());<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>A},c=<span style="color: #0000ff;"><span style="color: #0000ff;">function</span></span>(){<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>Uri (B ())};<span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span>{protocol:f,hasauthorityprefix:h,userinfo:i,host:j,port:k,path:l,query:m,anchor:n,setprotocol:o, Sethasauthorityprefix:p,setuserinfo:q,sethost:r,setport:s,setpath:t,setquery:u,setanchor:v,getqueryparamvalue: w,getqueryparamvalues:x,deletequeryparam:y,addqueryparam:z,replacequeryparam:a,tostring:b,clone:c}},jsuri=uri;</pre><span class="cnblogs_code_collapse"><span class="cnblogs_code_collapse">main method, Click to open the View</span></span><p><p></p></p><p><p><span style="color: #ff0000;">How to Use:</span></p></p><pre><pre><span style="color: #008000;">//</span> <span style="color: #008000;">This is the original URL</span> <span style="color: #0000ff;">var</span> sourceurl = "www.baidu.com?keyword= keyword"<span style="color: #000000;">;</span> <span style="color: #008000;">//</span> <span style="color: #008000;">calling a method to generate a new URL</span> <span style="color: #0000ff;">var</span> <span style="color: #0000ff;">New</span> Uri (sourceurl). Replacequeryparam ("keyword", "this is the new address after replacement"<span style="color: #000000;">)</span><span style="color: #008000;">//</span> <span style="color: #008000;"> effect display</span> alert ( Newurl.tostring ());</pre></pre><p><p></p></p><p><p><span style="color: #ff0000;">Execution Effect:</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="color: #ff0000;">method Interpretation</span> :<span style="color: #0000ff;">new Uri ("original URL address"). replacequeryparam ("parameter key", "value to replace"<span style="color: #000000;">);</span> </span></p></p><p><p><span style="color: #ff0000; background-color: #ffff00;">It's just so Simple oh. You don't have to judge the address bar for "?" ah, then get the parameters, cut, loop a lot of complex logic, 2 lines of code, can be used as a tool class.</span></p></p><p><p></p></p><p><p><span style="color: #ff0000; background-color: #ffff00;">-----------------------------uppercase end---------------------------</span></p></p><p><p></p></p><p><p>JS replace address bar parameter values</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.