JQuery Get URL Information

Source: Internet
Author: User

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=1var Source = $.url.attr ("source");//Http://localhost:19090/home/index?id=1var protocol = $.url.attr ("protocol");//HTTP all optional values HTTP, HTTPS, file, etcvar path = $.url.attr ("path");///home/indexvar host = $.url.attr ("host");//localhostvar port = $.url.attr ("port");//19090var relative = $.url.attr ("relative");///home/index?id=1var 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"); Span style= "color: #008000;" >// 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 example above are also called to handle shouting           

JQuery.URL.Parse source code is as follows:

//JQuery URL Parser//Written by Mark Perkins, [email protected]//license:http://unlicense.org/(i.e. do, 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,//The default URI is the page in which the script is runningStrictmode:False//' Loose ' parsing by defaultKey: ["Source", "protocol", "authority", "UserInfo", "User", "password", "host", "Port", "relative", "path", "directory", "File", "Query", "anchor"],//Keys available to queryQ: {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.name][$1] = $; } });ReturnUri };/** * 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.isemptyobject (parsed)) {setUp ();//If the URI has a not been parsed yet then does this first ...}if (key = = "Base") {if (Parsed.port!==Null && parsed.port!== "") {return Parsed.protocol + "://" + 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.isemptyobject (parsed)) {setUp ();//If the URI has a not 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 "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;ReturnThis; },/** * 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 ();ReturnThis; },/** * 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.*/Segmentfunction(POS) {If(Jquery.isemptyobject (parsed)) {setUp ();// If the URI has 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 public access to private ' param ' function-see above }; ();

JQuery Get URL Information

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.