jquery has a lot of ways to get URL information, but using this plugin is pretty cool.
Managed address in: Http://github.com/allmarkedup/jQuery-URL-Parser
http://localhost:19090/home/index?id=1 var Source = $.url.attr ("source"); http://localhost:19090/home/index?id=1 var protocol = $.url.attr ("protocol"); HTTP all optional values HTTP, HTTPS, file, etc var path = $.url.attr ("path"); /home/index var host = $.url.attr ("host"); localhost var port = $.url.attr ("port"); 19090 var relative = $.url.attr ("relative"); /home/index?id=1 var directory = $.url.attr ("directory");///home/index//http:// localhost:19090/floder/index.html?id=1 var file = $.url.attr ("file"); index.html var query = $.url.attr ("query"); ID = 1 This can also be called. Param () var id = $.url.param ("id"); 1; var segment = $.url.segment (1); 0:floder 1:index.html//You can also specify a different URL var seturlanchor = $.url.seturl ("http://www.baidu.com/#logo"). attr ("anchor"); At this point Seturlanchor and $.url of the above example also call the processing shout out
JQuery.URL.Parse source code is as follows:
JQuery URL parser//written by Mark Perkins, [email protected]//license:http://unlicense.org/(i.e. Want with it!) Jquery.url = function () {var segments = {}; var parsed = {}; /** * Options object. Only the URI and Strictmode values can changed via the setters below. */var options = {url:window.location,//default URI is the page in which the script is running strict Mode:false,//' loose ' parsing by default key: ["Source", "protocol", "authority", "UserInfo", "User", "password", "Host", "Port", "relative", "path", "directory", "file", "Query", "anchor"],//keys available to query Q: { Name: "Querykey", Parser:/(?: ^| &) ([^&=]*) =? ([^&]*)/g}, parser: {strict:/^ (?:( [^:\/?#]+):)? (?:\ /\/((?:( ([^:@]*):? ([^:@]*)) [email protected])? ([^:\/?#]*) (?::(\d*))?) ((((?:[^?#\/]*\/)*)([^?#]*))(?:\? ([^#]*))? (?:#(.*))?) /,//less intuitive, more accurate to the SPecs loose:/^ (?:(?! [^:@]+:[^:@\/]*@)([^:\/?#.] +):)? (?:\ /\/)? ((?:( ([^:@]*):? ([^:@]*)) [email protected])? ([^:\/?#]*) (?::(\d*))?) (((\/(?:[^?#](?! [^?#\/]*\. [^?#\/.] +(?:[?#]|$)))*\/?)? ([^?#\/]*)) (?:\? ([^#]*))? (?:#(.*))?) ///More intuitive, fails on relative paths and deviates from specs}}; /** * Deals with the parsing of the URI according to the regex above. * Written by Steven Levithan-see credits at top. */var Parseuri = function () {str = decodeURI (Options.url); var m = Options.parser[options.strictmode? "Strict": "Loose"].exec (str); var uri = {}; var i = 14; while (i--) {uri[options.key[i]] = M[i] | | ""; } Uri[options.q.name] = {}; Uri[options.key[12]].replace (Options.q.parser, function ($, $, $) {if ($) {uri[options.q.na ME][$1] = $; } }); return URI; }; /** * Returns The value of the passed in key from thE parsed URI. * * @param string key The key whose value is required */var key = function (key) {if (jquery.isemptyobje CT (parsed)) {setUp ();//If the URI has not been parsed yet and then does this first ...} if (key = = "Base") {if (Parsed.port!== null && parsed.port!== "") {return Parsed.pro Tocol + "://" + Parsed.host + ":" + Parsed.port + "/"; } else {return parsed.protocol + "://" + Parsed.host + "/"; }} return (Parsed[key] = = = "")? Null:parsed[key]; }; /** * Returns The value of the required query string parameter. * * @param string Item The parameter whose value is required */var param = function (item) {if (jquery.i Semptyobject (parsed)) {setUp ();//If the URI has no been parsed yet then does this first ...} Return (Parsed.querykey[item] = = = null)? Null:parsed.querykey[iteM]; }; /** * ' Constructor ' (not really!) function. * Called whenever the URI changes to kick off re-parsing of the URI and splitting it up into segments. */var setUp = function () {parsed = Parseuri (); Getsegments (); }; /** * Splits up the body of the URI into segments (i.e. sections delimited by '/') */var getsegments = function ( ) {var p = Parsed.path; segments = []; Clear out segments Array segments = Parsed.path.length = = 1? {}: (P.charat (p.length-1) = = "/"? P.substring (1, p.length-1): Path = p.substring (1)). Split ("/"); }; return {/** * Sets the parsing mode-either strict or loose. Set to loose by default. * * @param string mode the mode to set the parser to. Anything apart from a value of ' strict ' would set it to loose! */setmode:function (mode) {Options.strictmode = mode = = "strict"? True:false; return this; }, /** * Sets URI to parse if you don't want to parse the current page ' s URI. * Calling the function with no value for Newuri resets it to the current page ' s URI. * * @param string Newuri the URI to parse. */Seturl:function (Newuri) {Options.url = Newuri = = = undefined? Window.location:newUri; SetUp (); return this; },/** * Returns The value of the specified URI segment. Segments is numbered from 1 to the number of segments. * For example the URI of http://test.com/about/company/segment (1) would return ' about '. * * If No integer is passed to the function it returns the number of segments in the URI. * * @param int pos The position of the segment to return. Can be empty. */Segment:function (POS) {if (Jquery.isemptyobject (parsed)) {setUp ();//If the URI ha s not been parsed yet then does this first ... } if (pos = = = undefined) {return segments.length; } return (Segments[pos] = = = "" | | Segments[pos] = = = undefined)? Null:segments[pos]; }, Attr:key,///provides public access to private ' key ' function-see above Param:param//provides publi C access to private ' param ' function-see above}; ();