Many times in the front-end with JS to achieve the data interaction, than the background set of pages to achieve more simple, the project to achieve multiple filtering function, not made interface, it can only JS implementation of the URL splicing conversion Refresh page, the essence is to change the parameters in the Get parameter values, Here is a brief introduction to the property values in the Window.location
Property |
Describe |
Href |
The full URL |
Pathname |
Host name of the current URL |
Host |
Host name and port number of the current URL |
Port |
Port number of the current URL |
Hash |
URL starting from # (anchor) |
Search |
From? URL to start (Query section) |
Protocol |
Protocol for the current URL |
JS on the operation of the URL is nothing more than the operation of these property values, HTML custom properties, record the corresponding parameters and values
<div "> <a href=" javascript:void (0); "class=" Remove-sel selected "style=" padding-left:0; "type=" sentiment " > All </a> <a href= "javascript:void (0);" class= "Add-sel" type= "sentiment" value= "1" > Negative </a> <a href= "javascript:void (0);" class= "Add-sel" type= "sentiment" value= "2" > Neutral </a> <a href= "javascript:void (0 ); "class=" Add-sel "type=" sentiment "value=" 3 "> Front </a></div>
Add parameter
functionAddparama (Key, Val) {varSearch =Window.location.search; if(Search.indexof ('? ')! =-1) {Search= search.substring (1); varSearch_arr = Search.split (' & ')); varUrl_arr = []; varexists = 0; for(vari = 0; i < search_arr.length; i++) { if(!Search_arr[i])Continue; vartemp = search_arr[i].split (' = ')); if(Key = = Temp[0]) {exists= 1; Url_arr.push (Key+ ' = ' +val); } Else { if(temp[0]! = ' page ' && temp[0]! = ' token ') {Url_arr.push (search_arr[i]); } } } if(!exists) Url_arr.push (key+ ' = ' +val); varurl = window.location.pathname + '? ' + url_arr.join (' & ')); } Else { varurl = window.location.pathname + '? ' + key + ' = ' +Val; } window.location.href=URL; }
Delete parameter
functionDelparama (key) {varSearch =Window.location.search; varURL =window.location; if(Search.indexof (key)! =-1) {Search= search.substring (1); varSearch_arr = Search.split (' & ')); varUrl_arr = []; for(vari = 0; i < search_arr.length; i++) { vartemp = search_arr[i].split (' = ')); if(Key! = Temp[0]) {Url_arr.push (search_arr[i]); } } if(Url_arr.length = = 0) {URL=Window.location.pathname; }Else{URL= Window.location.pathname + '? ' + url_arr.join (' & ')); }} window.location.href=URL; }
JS implementation of URL splicing conversion