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!

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.


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

Dreamweaver Mac version
Visual web development tools

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

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
