当前位置:网站首页>Get parameters in URL
Get parameters in URL
2022-06-25 21:09:00 【Mr. Liu works hard】
Encapsulate a method
We know it well url The parameters in are displayed in this form
?id=123&name=tom&age=13…
The first one is : Basic usage Split with string
therefore . Come on, show it
function getParams(key) {
var result = {} // Define a global object
var str = window.location.search;
if(str.startsWith('?')) { // Judge str With ? At the beginning
var strParams = str.split('?')[1];
var arrParams = strParams.split('&');
// Then proceed for loop , It's directly used here forEach
arrParams.forEach((item) => {
var temKey = item.split('=')[0];
var temVal = item.split('=')[1];
result[temKey] = temVal
})
}
return result[key]
}
perhaps
getUrlParameter: function(field) {
// obtain Url Parameters
var GET = {};
var canshu=window.location.hash.split("?")[1]==undefined?'':window.location.hash.split("?")[1];
if(canshu!=''){
var strarr = canshu.split("&");
for(i=0;i<strarr.length;i++){
var xx = strarr[i];
var mykey = xx.split('=')[0];
var myvalue = xx.split('=')[1];
GET[mykey] = myvalue;
}
}
return GET[field];
}
The second kind :URLSearchParams
The method is JS Built in objects for , Can be mapped out search Query criteria
Use get Method , You can get the specified parameters
function get ParamsNew(key) {
var temData = new URLSearchParams(window.location.search);
return temData.get(key)
}

end
expand
When we make data changes , Suppose we want to get ID To show
var id = getParamsNew('id');
if(id){
// Yes id The data must be loaded when
loadData(); // Fill in the form with the data to be modified
};
// Method
function loadData() {
var strPeople = localStorage.getItem('myStudent'); // We from localStorage The value of
var people = [];
if(strPerple) {
people= JSON.parse(strPeople);
}
// If the arrow function has only one line , Don't write return
// We use... In the array find Method , Find the data of the review condition
var person = people.find((item) =>{
item.id == id // Find items that match the criteria
})
// Perform assignment operation
XXname.value = person.name;
XXage.value = person.age;
.....// So the assignment is successful
}
In this way, you can edit and display , However, you should pay attention to whether it is a combination of addition and modification , If you are in a piece, you need to add judgment conditions when saving
You can read this article https://blog.csdn.net/lzfengquan/article/details/118605300?spm=1001.2014.3001.5501
边栏推荐
- [summary] 2021unctf Campus (cry & MISC)
- Uncover n core 'black magic' of Presto + alluxio
- Attention to government and enterprise users! The worm prometei is spreading horizontally against the local area network
- What are the differences between domestic advanced anti DDoS servers and overseas advanced anti DDoS servers?
- What are cookies in Web site development?
- MySQL lock
- Online yaml to XML tool
- Install JDK, MySQL and nexus under Linux (tar package installation)
- 股市小白通过网上进行股票开户安全吗?
- Yunzhisheng atlas supercomputing platform: computing acceleration practice based on fluid + alluxio (Part 2)
猜你喜欢

Yolov4 improved version comes out! Yolov4 extended edition! Yolov4 launched scaled-yolov4: scaling cross stage partial network
Great changes in the interaction between people and the digital world
Cloud native 04: use envoy + open policy agent as the pre agent
Live broadcast preview | front line experts invite you to talk: the value of data science enabling multiple application scenarios
How does zhiting home cloud and home assistant access homekit respectively? What is the difference between them?
Boomfilter learning

Insert and update each database

js(3)
[distributed system design profile (1)] raft
2022 oceanbase technical essay contest officially opened | come and release your force
随机推荐
[distributed system design profile (1)] raft
Decipher the AI black technology behind sports: figure skating action recognition, multi-mode video classification and wonderful clip editing
银河证券靠谱吗?开证券账户安全吗?
股市小白通过网上进行股票开户安全吗?
ThreadLocal class
Those high-frequency and real software test interview questions sorted out by the test director in 7 days, come to get
04 disk space management
[deep learning series] - visual interpretation of neural network
Win10 common software
Feature Engineering in simple terms – a practice guide based on openmldb (Part 1)
Yolov4 improved version comes out! Yolov4 extended edition! Yolov4 launched scaled-yolov4: scaling cross stage partial network
Online yaml to XML tool
Lesson 3 urllib
After 20 days' interview, I finally joined Ali (share the interview process)
Mysqldumpslow out slow statements in the database
From URL to access page rendering
Vbpr (visual Bayesian personalized ranking) paper summary
Install JDK, MySQL and nexus under Linux (tar package installation)
JS__ Prototype, prototype chain, call/apply__ Duyi
HMS core actively explores the function based on hardware ear return, helping to reduce the overall singing delay rate of the singing bar by 60%