Jquery

var getUrlParameter = function getUrlParameter(sParam) { var sPageURL = window.location.search.substring(1), sURLVariables = sPageURL.split("&"), sParameterName, i; for (i = 0; i And this is how you can use this function assuming the URL is,http://dummy.com/?technology=jquery&blog=jquerybyexample.

Bạn đang xem: Jquery

var tech = getUrlParameter("technology");var blog = getUrlParameter("blog");

*

*

Solution from 2020

We have: http://example.com?sent=yes

let searchParams = new URLSearchParams(window.location.search)Does sent exist?

searchParams.has("sent") // trueIs it equal to lớn "yes"?

let param = searchParams.get("sent")and then just compare it.


*

jQuery code snippet to get the dynamic variables stored in the url as parameters and store them as JavaScript variables ready for use with your scripts:

$.urlParam = function(name) 0;example.com?param1=name¶m2=&id=6

$.urlParam("param1"); // name$.urlParam("id"); // 6$.urlParam("param2"); // nullexample params with spaces

http://www.jquery4u.com?city=Gold Coastconsole.log($.urlParam("city")); //output: Gold%20Coastconsole.log(decodeURIComponent($.urlParam("city"))); //output: Gold Coast
Share
Follow
edited Oct 31 "18 at 5:03
davidrl1000
28111 gold badge33 silver badges1515 bronze badges
answered Aug 18 "14 at 8:39
Reza Baradaran GazorisangiReza Baradaran Gazorisangi
4,84733 gold badges2222 silver badges2323 bronze badges
12
| Show 7 more comments
95
I always stick this as one line. Now params has the vars:

params=;location.search.replace(/+(<^=&>+)=(<^&>*)/gi,function(s,k,v)params=v)multi-lined:

var params=;window.location.tìm kiếm .replace(/+(<^=&>+)=(<^&>*)/gi, function(str,key,value) params = value; );as a function

function getSearchParams(k) var p=; location.search.replace(/+(<^=&>+)=(<^&>*)/gi,function(s,k,v)p=v) return k?p:p;which you could use as:

getSearchParams() //returns key1:val1, key2:val2or

getSearchParams("key1") //returns val1
Share
Follow
edited Feb 28 "17 at 22:36
answered Nov 4 "14 at 20:35
AwokeKnowingAwokeKnowing
6,21066 gold badges3131 silver badges4343 bronze badges
9
| Show 4 more comments
50
Yet another alternative sầu function...

function param(name) "").split("&")<0>;
Share
Follow
answered Sep 29 "16 at 10:51
rodnaphrodnaph
1,15999 silver badges1111 bronze badges
5
Add a phản hồi |
47
May be its too late. But this method is very easy và simple


Share
Follow
edited Nov 18 "14 at 12:49
kiamlaluno
24.5k1616 gold badges7070 silver badges8585 bronze badges
answered Jan 8 "14 at 10:01
Tarun GuptaTarun Gupta
5,85122 gold badges3535 silver badges3838 bronze badges
Add a phản hồi |
5
This might be overkill, but there is a pretty popular library now available for parsing URIs, called URI.js.

Example


var uri = "http://example.org/foo.html?technology=jquery&technology=css&blog=obatambeienwasirherbal.com";var components = URI.parse(uri);var query = URI.parseQuery(components<"query">);document.getElementById("result").innerHTML = "URI = " + uri;document.getElementById("result").innerHTML += "công nghệ = " + query<"technology">;// If you look in your console, you will see that this library generates a JS array for multi-valued queries!console.log(query<"technology">);console.log(query<"blog">);
Share
Follow
answered Feb 13 "16 at 20:28
alexwalexw
6,94666 gold badges4545 silver badges8080 bronze badges
Add a phản hồi |
5
function GetRequestParam(param)var res = null;tryvar qs = decodeURIComponent(window.location.tìm kiếm.substring(1));//get everything after then "?" in URIvar ar = qs.split("&");$.each(ar, function(a, b)var kv = b.split("=");if(param === kv<0>)res = kv<1>;return false;//break loop);catch(e)return res;
Share
Follow
answered Mar 24 "17 at 6:47
p_champp_champ
85511 gold badge1111 silver badges20đôi mươi bronze badges
Add a comment |
4
Try this working demo http://jsfiddle.net/xy7cX/

API:

This should help :)

code

var url = "http://myurl.com?sent=yes"var pieces = url.split("?");alert(pieces<1> + " ===== " + $.inArray("sent=yes", pieces));
Share
Follow
answered Oct 21 "13 at 10:00
Tats_innitTats_innit
33.1k99 gold badges6666 silver badges7474 bronze badges
1
Add a comment |
4
So simple you can use any url và get value

function getParameterByName(name, url) &Usage Example

// query string: ?first=value1&second=&value2var foo = getParameterByName("first"); // "value1"var bar = getParameterByName("second"); // "value2" Note: If a parameter is present several times (?first=value1&second=value2), you will get the first value (value1) và second value as (value2).


Share
Follow
answered Sep 16 "16 at 6:19
ImBhavin95ImBhavin95
1,38522 gold badges1414 silver badges2525 bronze badges
Add a bình luận |
3
There"s this great library:https://github.com/allmarkedup/purl

which allows you khổng lồ do simply

url = "http://example.com?sent=yes";sent = $.url(url).param("sent");if (typeof sent != "undefined") // sent exists if (sent == "yes") // sent is equal khổng lồ yes // ... The example is assuming you"re using jQuery. You could also use it just as plain javascript, the syntax would then be a little different.


Share
Follow
answered Mar 23 "15 at 20:50
Michael KonečnýMichael Konečný
1,39811 gold badge1414 silver badges20trăng tròn bronze badges
1
Add a comment |
3
http://example.com?sent=yesBest solution here.

function getUrlParameter(name) name = name.replace(/<<>/, "\<").replace(/<>>/, "\>"); var regex = new RegExp("<\?&>" + name + "=(<^&#>*)"); var results = regex.exec(location.href); return results === null ? "" : decodeURIComponent(results<1>.replace(/+/g, " "));;With the function above sầu, you can get individual parameter values:

getUrlParameter("sent");
Share
Follow
edited Jan 6 "19 at 15:52
Elia Weiss
5,22988 gold badges4848 silver badges7878 bronze badges
answered Sep 19 "18 at 16:14
NaamiNaamày
34133 silver badges99 bronze badges
1
Add a comment |
2
This will give you a nice object lớn work with

function queryParameters () &/); params.forEach( function(it) if (it) var param = it.split("="); result> = param<1>; ); return result; And then;

if (queryParameters().sent === "yes") { .....
Share
Follow
edited Aug 22 "14 at 5:25
answered Aug 22 "14 at 5:19
Brian FBrian F
1,4081313 silver badges2121 bronze badges
1
Add a comment |
2
This is based on Gazoris"s answer, but URL decodes the parameters so they can be used when they contain data other than numbers and letters:

function urlParam(name) var results = new RegExp("" + name + "=(<^&#>*)").exec(window.location.href); // Need to decode the URL parameters, including putting in a fix for the plus sign // https://obatambeienwasirherbal.com/a/24417399 return results ? decodeURIComponent(results<1>.replace(/+/g, "%20")) : null;
Share
Follow
edited May 23 "17 at 11:54
Community♦
111 silver badge
answered Jun 23 "16 at 10:07
Stephen OstermillerStephen Ostermiller
18.3k88 gold badges7373 silver badges9393 bronze badges
Add a comment |
2
There is another example with using URI.js library.

Xem thêm: Tải Game Nba Live Hack Download, Nba Live Asia 4

Example answers the questions exactly as asked.


var url = "http://example.com?sent=yes";var urlParams = new URI(url).search(true);// 1. Does sent exist?var sendExists = urlParams.sent !== undefined;// 2. Is it equal to "yes"?var sendIsEqualtToYes = urlParams.sent == "yes";// output results in readable form// not required for productionif (sendExists) console.log("Url has "sent" param, its value is "" + urlParams.sent + """); if (urlParams.sent == "yes") console.log(""Sent" param is equal to lớn "yes""); else console.log(""Sent" param is not equal lớn "yes""); else console.log("Url hasn"t "sent" param");
Share
Follow
answered Oct 19 "16 at 2:37
userlonduserlond
3,30322 gold badges2828 silver badges4545 bronze badges
Add a phản hồi |
1
Coffeescript version of Sameer"s answer

getUrlParameter = (sParam) -> sPageURL = window.location.search.substring(1) sURLVariables = sPageURL.split("&") i = 0 while i
Share
Follow
answered Mar 6 "15 at 17:15
stevenspielstevenspiel
4,60099 gold badges5050 silver badges8181 bronze badges
Add a phản hồi |
1
A slight improvement to lớn Sameer"s answer, cađậy params inlớn closure lớn avoid parsing & looping through all parameters each time calling

var getURLParam = (function() { var paramStr = decodeURIComponent(window.location.search).substring(1); var paramSegs = paramStr.split("&"); var params = <>; for(var i = 0; i
Share
Follow
answered Jul 4 "15 at 16:00
streaver91streaver91
73411 gold badge77 silver badges1010 bronze badges
Add a phản hồi |
1
I use this and it works.http://codesheet.org/codesheet/NF246Tzs

function getUrlVars() var vars = ; var parts = window.location.href.replace(/+(<^=&>+)=(<^&>*)/gi, function(m,key,value) vars = value; );return vars;var first = getUrlVars()<"id">;
Share
Follow
answered Feb 13 "16 at 22:29
studio-klikstudio-klik
16811 silver badge33 bronze badges
Add a phản hồi |
1
With vanilla JavaScript, you could easily take the params (location.search), get the substring (without the ?) & turn it into an array, by splitting it by "&".

As you iterate through urlParams, you could then split the string again with "=" & add it lớn the "params" object as object> = element<1>. Super simple và easy khổng lồ access.

http://www.website.com/?error=userError&type=handwritten

var urlParams = location.search.substring(1).split("&"), params = ; urlParams.forEach(function(el) var tmpArr = el.split("="); params> = tmpArr<1>; ); var error = params<"error">; var type = params<"type">;
Share
Follow
answered May 6 "16 at 14:45
DDTDDT
24633 silver badges1111 bronze badges
Add a phản hồi |
1
What if there is và in URL parameter like filename="p&g.html"&uid=66

In this case the 1st function will not work properly. So I modified the code

function getUrlParameter(sParam) { var sURLVariables = window.location.search.substring(1).split("&"), sParameterName, i; for (i = 0; i
Share
Follow
edited May đôi mươi "16 at 19:27
answered May đôi mươi "16 at 19:03
user562451user562451
6511 silver badge88 bronze badges
Add a comment |
1
Admittedly I"m adding my answer lớn an over-answered question, but this has the advantages of:

-- Not depending on any outside libraries, including jQuery

-- Not polluting global function namespace, by extending "String"

-- Not creating any global data và doing unnecessary processing after match found

-- Handling encoding issues, và accepting (assuming) non-encoded parameter name

-- Avoiding explicit for loops

String.prototype.urlParamValue = function() var desiredVal = null; var paramName = this.valueOf(); window.location.search.substring(1).split("&").some(function(currentValue, _, _) var nameVal = currentValue.split("="); if ( decodeURIComponent(nameVal<0>) === paramName ) desiredVal = decodeURIComponent(nameVal<1>); return true; return false; ); return desiredVal;;Then you"d use it as:

var paramVal = "paramName".urlParamValue() // null if no match
Share
Follow
answered Jul 30 "16 at 19:37
BaseZenBaseZen
8,16622 gold badges3030 silver badges4444 bronze badges
Add a phản hồi |
1
If you want khổng lồ find a specific parameter from a specific url:

function findParam(url, param) var check = "" + param; if(url.search(check )>=0) return url.substring(url.search(kiểm tra )).split("&")<0>.split("=")<1>; var url = "http://www.yourtên miền.com/example?id=1&order_no=114&invoice_no=254"; alert(findParam(url,"order_no"));
Share
Follow
edited Mar 19 "18 at 17:07
answered Mar 16 "18 at 9:56
Wahid MasudWahid Masud
87388 silver badges2525 bronze badges
Add a comment |
0
Another solution that uses jQuery và JSON, so you can access the parameter values through an object.

var loc = window.location.href;var param = ;if(loc.indexOf("?") > -1){ var params = loc.substr(loc.indexOf("?")+1, loc.length).split("&"); var stringJson = "{"; for(var i=0;iAssuming your URL is http://example.com/?search=hello+world&language=en&page=3

After that it"s only a matter of using the parameters lượt thích this:

param.languageto lớn return

enThe most useful usage of this is to run it at page load & make use of a global variable to use the parameters anywhere you might need them.

If your parameter contains numeric values then just parse the value.

parseInt(param.page)If there are no parameters param will just be an empty object.

Xem thêm: cai dat trinh xem html


Share
Follow
edited Oct 26 "17 at 12:49
answered Oct 26 "17 at 12:38
NiksuskiNiksuski
4544 bronze badges
Add a phản hồi |
-1
$.urlParam = function(name) 0;
Share
Follow
edited Nov 18 "14 at 12:50
kiamlaluno
24.5k1616 gold badges7070 silver badges8585 bronze badges
answered Nov 18 "14 at 9:39
Aftab UddinAftab Uddin
14911 silver badge33 bronze badges
1
Add a bình luận |
-1
use this

$.urlParam = function(name) var results = new RegExp("" + name + "=(<^&#>*)").exec(window.location.href); return results<1>
Share
Follow
answered Atruyền thông quảng cáo 24 "15 at 9:16
ddfsfddfsf
711 bronze badge
Add a bình luận |
1
2 Next
Highly active sầu question. Earn 10 reputation in order to lớn answer this question. The reputation requirement helps protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged jquery url parameters query-string querystringparameter or ask your own question.


The Overflow Blog
Featured on Meta
Linked
-1
Split text from url
0
Jquery getting a url id
0
Extracting id value from a facebook url
0
How lớn get one paramater value from url in jquery?
-1
Get URL Parameter using Jquery
-1
skết thúc get request via jquery slider & keep the value on slider on after page reloads
0
How to lớn change currency by changing the URL?
0
how to get id from current url & make new url in ajax
0
How to query the url paramter using jquery?
2695
How can I get query string values in JavaScript?
See more linked questions
Related
8045
How do I kiểm tra if an element is hidden in jQuery?
2581
Encode URL in JavaScript?
1931
Get current URL with jQuery?
2589
How do I modify the URL without reloading the page?
2472
Set a default parameter value for a JavaScript function
2695
How can I get query string values in JavaScript?
4800
How vì chưng I check whether a checkbox is checked in jQuery?
3226
Get the current URL with JavaScript?
4375
How lớn change the URI (URL) for a remote Git repository?
1557
How are parameters sent in an HTTP POST request?
Hot Network Questions more hot questions
Question feed
Subscribe to lớn RSS
Question feed To subscribe khổng lồ this RSS feed, copy and paste this URL into your RSS reader.


lang-js
Staông xã Overflow
Products
Company
Stack Exchange Network
site design / biểu tượng logo © 2021 Stachồng Exchange Inc; user contributions licensed under cc by-sa. rev2021.3.31.38952


Stack Overflow works best with JavaScript enabled
*

Your privacy

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device & discchiến bại information in accordance with our Cookie Policy.


Chuyên mục: