码迷,mamicode.com
首页 > Web开发 > 详细

uri.js的用法事例

时间:2017-02-15 18:24:55      阅读:280      评论:0      收藏:0      [点我收藏+]

标签: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.js

JavaScript
<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.html

url;
// 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 Values

JavaScript
// Querystring values
url = new URI("http://www.ecommerce.com/product.html?customerId=27&checkout=true");

// Get querystring part from URL
url.query();
// returns "customerId=27&checkout=true"

// Get Querystring value as JSON object
url.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 Values

JavaScript
url = new URI("http://www.ecommerce.com/product.html");

//set customerId as Querystring
url.addQuery("customerId", "27");
//returns "http://www.ecommerce.com/product.html?customerId=27"

//Remove customerId as Querystring
url.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的用法事例

标签:protoc   rod   port   check   using   dial   returns   cat   int   

原文地址:http://www.cnblogs.com/ys-wuhan/p/6402378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!