Examples of usages of uri.js

Source: Internet
Author: User

From: http://smoothprogramming.com/tutorials/get-set-query-string-values-from-url-using-uri-js/

Get or Set Query String Values from URL using Uri.js

Uri.js is a mature JavaScript library for manipulation of URI. Uri.js provides methods to get or set query string values the using JavaScript functions on browsers.

Download uri.js or URI.min.js from official Uri.js Github Repository or build it from here. Add it into the your HTML page for using the it in your script.

function to get Query String value from URL using Uri.jsjavascript
<script type="Text/javascript" src="/js/uri.js"></script>functionGetquerystringvalue(Queryparam){Get current URL.var Currenturl=NewUri();If Queryparam is in the querystring of CurrenturlIf(Currenturl.Hasquery(Queryparam)){Get all QueryString values as a JSON objectvar qsvalues= Currenturl.Query(True);//return queryparam ' s value return qsvalues [queryparam] Span class= "token punctuation" > else { Span class= "token comment" >//Queryparam is isn't in the querystring. So return as undefined. return undefined}}//If currenturl="/http www.ecommerce.com/product.html?customerId=27 "Then,//getquerystringvalue (" CustomerId ") returns" //getquerystringvalue ("Notpresentqueryparam") returns Undefined                
Uri.js Introduction

Uri.js offers methods for manipulating URLs. Please see below code to get a intro on few possible operations with Uri.js.

Javascript
Get current URLs from the browser bar.var URL=NewUri();Return Http://smoothprogramming.com/tutorials/get-set-query-string-values-from-url-using-uri-jsThis was equivalent to Window.location.href command in Js.url=NewUri("Http://www.smoothprogramming.com:80/tutorials/get-set-query-string-values-from-url-using-uri-js.html");Sets URL to Http://www.smoothprogramming.com:80/tutorials/get-set-query-string-values-from-url-using-uri-js.htmlurl;Return "http://www.smoothprogramming.com:80/tutorials/get-set-query-string-values-from-url-using-uri-js.html" Url.Protocol());Return "http" url.Origin();Return "http://www.smoothprogramming.com:80" url.Hostname());Return "www.smoothprogramming.com" url.Host());Return "www.smoothprogramming.com:80" url.Port());Return "+" url.path (; Return "/tutorials/get-set-query-string-values-from-url-using-uri-js.html" Url . directory (; Return "/tutorials" Url. Filename//return "get-set-query-string-values-from-url-using-uri-js.html"     
Get Query String Valuesjavascript
Querystring Valuesurl=NewUri("Http://www.ecommerce.com/product.html?customerId=27&checkout=true");Get QueryString part from Urlurl.Query();Returns "Customerid=27&checkout=true"Get Querystring value as JSON Objecturl.Query(True);Returns "{" CustomerId ":" "," "Checkout": "True"} "Is customerId in the QueryString list? URL.Hasquery("CustomerId");Returns TrueIs dummyquerystr in the QueryString list? URL.Hasquery("Dummyquerystr"); //returns false//is customerId value = 27?url.  Hasquery("CustomerId", "the");  Returns True//is customerId value = 50?url.  Hasquery("CustomerId", "" ");  Returns false                
Set Query String Valuesjavascript
Url=NewUri("Http://www.ecommerce.com/product.html"; Set CustomerId as Querystringurl. Addquery ( ") ; //returns "http://www.ecommerce.com/product.html?customerId=27" //remove customerId as Querystringurl.removequery" customerId "" ; Returns "Http://www.ecommerce.com/product.html"         


Online Example: Http://codepen.io/hiralbest/pen/kXwPKP

Conclusion

This post has the useful and important list of methods to manipulate URLs using Uri.js. If you is interested in detail documentation of all uri.js then, please refer Uri.js documentation Page.

References

Uri.js
Uri.js Github Repo

Examples of usages of uri.js

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.