How to implement object-based management of URLs in js
This article mainly introduces the relevant knowledge and usage of url object management in js. Friends in need can learn together.
1. Problem description
url is an indispensable value that needs to be dealt with in the web writing process, whether it is jumping from page to page. In the transfer, it is still ajax request data or the url provided by other framework plug-ins.
For many programmers, js often encounters the need to change the url (mainly the parameters contained in it). Most people use The method is to directly splice.
This method is better than simplicity, but it also has many shortcomings, such as:
There are always potential dangers in the security of the URL formed by splicing;
Obtaining the parameters and pure address contained in a complete URL for the next step of comparison is also a troublesome matter;
2. Solution idea
Based on the above problems, my solution strategy is to objectify the url, and put the pure url address and url parameters into each attribute of an object.
Each time the url is To change, you can first analyze it into the object format, then change some of the parameters, and then construct it into a new url method.
When you start building it like this, you may feel that it is a bit unnecessary, but when dealing with some more complex situations , will be very convenient.
3. Demonstration code
First provides a method to analyze and construct the url (you can consider encapsulating it into a method , the method name can be more complicated to avoid duplication):
/** * 数据处理-解析url为一个对象 */ function parseUrl(strUrl){ var arrUrlPart=strUrl.split('?'); var strUrl=arrUrlPart[0]; var mUrl={ url:strUrl }; if(arrUrlPart.length===2){ var strParam=arrUrlPart[1]; var arrParamPart=strParam.split('&'); for(i in arrParamPart){ var strParamPart=arrParamPart[i]; var arrParamKy=strParamPart.split('='); var strKey=arrParamKy[0]; var strValue=decodeURIComponent(arrParamKy[1]); mUrl[strKey]=strValue; } } return mUrl; } /** * 数据处理-构成/组建url(字符串) */ function concatUrl(mUrl){ var strUrl=mUrl.url; var strParam=''; for(strKey in mUrl){ if(strKey==='url'||mUrl[strKey]===null) continue; strParam+=(strKey+'='+encodeURIComponent(mUrl[strKey])+'&');//注入避免 } if(strParam!==''){ strParam=('?'+strParam.substring(0,strParam.length-1)); } return strUrl+strParam; }
The following are usage examples. Of course, they only show relatively simple situations and may not fully reflect the power of url object management:
var strUrl1='www.example.com/admin/product/main?group_code=test_group&p_code=shangpin1'; var mUrl1=parseUrl(strUrl1); console.log(mUrl1.p_code); mUrl1.p_code='shangpin2'; var strUrl2=concatUrl(mUrl1); console.log(strUrl2); mUrl1.group_code=null; mUrl1.user_name='用?&=户'; var strUrl3=concatUrl(mUrl1); console.log(strUrl3); var mUrl3=parseUrl(strUrl3); console.log(mUrl3.user_name);
The printed result is:
shangpin1 www.example.com/admin/product/main?group_code=test_group&p_code=shangpin2 www.example.com/admin/product/main?p_code=shangpin2&user_name=%E7%94%A8%3F%26%3D%E6%88%B7 用?&=户
In the above cases, especially case 3, it can be said that the URL conversion function is very flexible.
Of course, when actually using it, for safety For the sake of safety, when generating a new URL, a new object is usually created first instead of modifying the original object.
4. Areas to be improved
The above situation applies to non-path parameters. When using path parameters, such as:
www.example.com/admin/product/list/1
This 1 is used as a parameter, which is not applicable in this method.
The method can also be optimized and transformed into an analysis and reconstruction method suitable for path parameters, which is another story later.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
The problem of off-canvas sliding beyond the part in the mui framework
Using better-scroll in vue2.0 How to realize mobile terminal sliding
Using cli mui in Vue related area scrolling issues
Comparison of the use of express and koa (detailed tutorial)
Online paid courses in vue (detailed tutorial)
The above is the detailed content of How to implement object-based management of URLs in js. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

How to use JS and Baidu Map to implement map pan function Baidu Map is a widely used map service platform, which is often used in web development to display geographical information, positioning and other functions. This article will introduce how to use JS and Baidu Map API to implement the map pan function, and provide specific code examples. 1. Preparation Before using Baidu Map API, you first need to apply for a developer account on Baidu Map Open Platform (http://lbsyun.baidu.com/) and create an application. Creation completed

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

Overview of how to use JS and Baidu Maps to implement map click event processing: In web development, it is often necessary to use map functions to display geographical location and geographical information. Click event processing on the map is a commonly used and important part of the map function. This article will introduce how to use JS and Baidu Map API to implement the click event processing function of the map, and give specific code examples. Steps: Import the API file of Baidu Map. First, import the file of Baidu Map API in the HTML file. This can be achieved through the following code:

How to use JS and Baidu Maps to implement the map heat map function Introduction: With the rapid development of the Internet and mobile devices, maps have become a common application scenario. As a visual display method, heat maps can help us understand the distribution of data more intuitively. This article will introduce how to use JS and Baidu Map API to implement the map heat map function, and provide specific code examples. Preparation work: Before starting, you need to prepare the following items: a Baidu developer account, create an application, and obtain the corresponding AP
