Home > Article > Web Front-end > jquery gets url and url parameters (with code)
This time I will bring you jquery to get url and url parameters (with code). What are the precautions for jquery to get url and url parameters? The following is a practical case, let's take a look.
1. It is very simple to get the url with jquery. The code is as follows
window.location.href;In fact, it just uses the basic javascript
window object, no knowledge of jquery
2. jquery is more complicated to obtain url parameters, andregular expressions are used, so it is important to learn javascript regular expressions
First, let’s look at how to get a certain parameter in the url simply through javascript.function getUrlParam(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象 var r = window.location.search.substr(1).match(reg); //匹配目标参数 if (r!=null) return unescape(r[2]); return null; //返回参数值 }You can get the value of the parameter by passing the parameter name in the url through this function. For example, the url is http://www.xxx.loc/admin/write-post.php?cid=79If we want to get the value of cid, we can write like this:
getUrlParam('cid');Understand the javascript acquisition url parameter method, we can use this method to extend a method for jquery to obtain url parameters through jquery. The following code extends a getUrlParam() method for jquery
(function($){ $.getUrlParam = function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r!=null) return unescape(r[2]); return null; } })(jQuery);After extending this method for jquery, we You can get the value of a certain parameter through the following method
$.getUrlParam('cid');I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website! Recommended reading:
How to deal with page anchor failure in iframe
Detailed explanation of the steps to obtain the document object in iframe
The above is the detailed content of jquery gets url and url parameters (with code). For more information, please follow other related articles on the PHP Chinese website!