uri.js的用法事例
来源于: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
<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.
// 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
// 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
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的用法事例的更多相关文章
- Javascript模块化编程(三):require.js的用法
Javascript模块化编程(三):require.js的用法 原文地址:http://www.ruanyifeng.com/blog/2012/11/require_js.html 作者: 阮一峰 ...
- JS 正则表达式用法
JS 正则表达式用法简介 简单的说,正则表达式是一种可以用于模式匹配和替换的强有力的工具.其作用如下: 测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或 ...
- toastr.js插件用法
toastr.js插件用法 toastr.js是一个基于jQuery的非阻塞通知的JavaScript库.toastr.js可以设定四种通知模式:成功.出错.警告.提示.提示窗口的位置.动画效果等都可 ...
- Js相关用法个人总结
Js相关用法个人总结 js中将数组元素添加到对象中var obj = {}; var pushArr = [11,22,33,44,55,66]; for(var i=0;i<pushArr. ...
- jQuery each和js forEach用法比较
本文实例分析了jQuery each和js forEach用法.分享给大家供大家参考,具体如下: 对于遍历数组的元素,js代码和jquery都有类似的方法,js用的是forEach而jquery用的是 ...
- 【JS库】URI.js
做前端的,应该有不少人都写过操作URL的代码,比如提取问号后面的参数.或者主机名什么的,比如这样: var url="http://jszai.com/foo?bar=baz", ...
- Javascript模块化编程require.js的用法
JS模块化工具requirejs教程(一):初识requirejs http://www.runoob.com/w3cnote/requirejs-tutorial-1.html JS模块化工具req ...
- (转)Javascript模块化编程(三):Require.js的用法
转自 ruanyifeng 系列目录: Javascript模块化编程(一):模块的写法 Javascript模块化编程(二):AMD规范 Javascript模块化编程(三):Require.js的 ...
- 【笔记】关于require.js 的用法
最近忙于学校的一个新网站建设,对于以前的前端程序编写方式的不正规特意上网学习了require.js 的用法,使此次的工程更加有条理同时符合当前前端的开发模式——前端模块化. 网上有不少很好的学习文章这 ...
随机推荐
- 图形数据库 Neo4j 开发实战
https://www.ibm.com/developerworks/cn/java/j-lo-neo4j/ Neo4j 是一个高性能的 NoSQL 图形数据库.Neo4j 使用图(graph)相关的 ...
- AI 也开源:50 大开源 AI 项目 (转)
这些开源AI项目专注于机器学习.深度学习.神经网络及其他应用场合. 自IT界早期以来,研制出能像人类那样“思考”的机器一直是研究人员的一大目标.在过去几年,计算机科学家们在人工智能(AI)领域已取得了 ...
- oauth2-server-php-docs 存储 学说2
学说2 创建客户端和访问令牌存储 要把学说融入到你的项目中,首先要建立你的实体.我们先从客户端,用户和访问令牌模型开始: yaml YourNamespace\Entity\OAuthClient: ...
- 解剖 CPU(另)
http://itbbs.pconline.com.cn/notebook/11026377.html 话不多说,这个处理器,就是今天我们要厮杀的对象! 1. 案板上的她,静静等等手术的进行! 2. ...
- 微软BI 之SSAS 系列 - 实现Cube 以及角色扮演维度,度量值格式化和计算成员的创建
在熟悉完下面这三种维度的创建方式之后,就可以开始创建我们的第一个 Cube 了. SSAS 系列 - 自定义的日期维度设计 SSAS 系列 - 基于雪花模型的维度设计 SSAS系列 - 关于父子维度的 ...
- HOW TO: Synchronize changes when completing a P2V or V2V with VMware vCenter Converter Standalone 5.1
http://www.experts-exchange.com/Software/VMWare/A_11489-HOW-TO-Synchronize-changes-when-completing-a ...
- 使用electron+Js开发夸平台(Linux、Win、Mac)的桌面应用程序
一.开源地址: https://github.com/electron/electron 二.官方网站: https://electron.atom.io/ 三.案例
- java.net.ConnectException: Connection refused: no further information
NIO项目中出现了这个错误: java.net.ConnectException: Connection refused: no further information 一般是因为InetSocket ...
- 算法笔记_199:第二届蓝桥杯软件类决赛真题(C语言本科)
前言:以下代码部分仅供参考,C语言解答部分全部来自网友,Java语言部分部分参考自网友,对于答案的正确性不能完全保证. 试题1 数论中有著名的四方定理:所有自然数至多只要用四个数的平方和就可以表示. ...
- 关于sendtoback()和bringtofront() 的理解
如下的代码: button2.Dock = DockStyle.Top; button1.SendToBack(); button1.Dock = DockStyle.Top; button3.Doc ...