JS implementation of URL splicing conversion

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.