uri.js的用法案例

來源:互聯網
上載者:User

標籤:protoc   rod   port   check   using   dial   returns   cat   int   

來源於: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 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 your html page for using it in your script.

function to get Query String value from URL using URI.jsJavaScript
<script type="text/javascript" src="/js/URI.js"></script>function getQueryStringValue(queryParam){ // Get current URL. var currentURL = new URI(); // If queryParam is in the querystring of currentURL if(currentURL.hasQuery(queryParam)){ // Get all querystring values as a json object var qsValues = currentURL.query(true); // return queryParam‘s value return qsValues[queryParam]; } else { // queryParam is not in the querystring. So return as undefined. return undefined; }}// If currentURL="http://www.ecommerce.com/product.html?customerId=27" then,// getQueryStringValue("customerId") returns "27"// getQueryStringValue("notPresentQueryParam") returns undefined
URI.js Introduction

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

JavaScript
// Get current URL from the browser bar.var url = new URI();// return http://smoothprogramming.com/tutorials/get-set-query-string-values-from-url-using-uri-js// This is equivalent to window.location.href command in js.url = new URI("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 "80"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 = new URI("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":"27","checkout":"true"}"//Is customerId in the querystring list ?url.hasQuery("customerId");// returns true//Is dummyQuerystr in the querystring list ?url.hasQuery("dummyQueryStr");// returns false// Is customerId value = 27?url.hasQuery("customerId", "27");// returns true//is customerId value = 50?url.hasQuery("customerId", "50");// returns false
Set Query String ValuesJavaScript
url = new URI("http://www.ecommerce.com/product.html");//set customerId as Querystringurl.addQuery("customerId", "27");//returns "http://www.ecommerce.com/product.html?customerId=27"//Remove customerId as Querystringurl.removeQuery("customerId");// returns "http://www.ecommerce.com/product.html"


線上執行個體:http://codepen.io/hiralbest/pen/kXwPKP

Conclusion

This post has only most useful and important list of methods to manipulate URL using URI.js. If you are interested in detail documentation of all URI.js then, please refer URI.js Documentation Page.

References

URI.js
URI.js Github Repo

uri.js的用法案例

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.