Created
August 7, 2012 12:59
-
-
Save Ultrabenosaurus/3285106 to your computer and use it in GitHub Desktop.
Search the current querystring for a variable or variable/value pair and remove if found.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function query_string_remove(find, val){ | |
| // get query string, remove ? and split on & | |
| qryStr = window.location.search.replace("?", ""); | |
| qryPrts = qryStr.split('&'); | |
| // assume search will fail, for return value | |
| found = false; | |
| // relies on jQuery for simplicity of this function | |
| // use your own foreach function if you prefer | |
| jQuery.each(qryPrts, function(i, v) { | |
| // don't try to operate on empty array item | |
| if(v !== undefined){ | |
| // split on = to make comparisons easier | |
| var tmpQry = v.split("="); | |
| // check if user wants to look for a specific value | |
| if(val === '' || val === null || typeof val === 'undefined'){ | |
| // assuming, they don't care about value | |
| if(tmpQry[0] == find){ | |
| // remove item if found | |
| qryPrts.splice(i, 1); | |
| // set return value to true | |
| found = true; | |
| } | |
| } else { | |
| // if they want to remove only on a specific value | |
| if(tmpQry[0] == find && tmpQry[1] == val) { | |
| // remove item if found | |
| qryPrts.splice(i, 1); | |
| // set return value to true | |
| found = true; | |
| } | |
| } | |
| } | |
| }); | |
| // if item was removed, replace original query with joined array | |
| if(found){ | |
| qryStr = qryPrts.join('&'); | |
| } | |
| // return array of true/false, processed query and search parameters | |
| results = [found, ('?'+qryStr), find, val]; | |
| return results; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset='utf-8' /> | |
| <title>JS Test</title> | |
| <script type='text/javascript' src='jquery.js'></script> | |
| </head> | |
| <body> | |
| <ul> | |
| <li><a href="?foo=bar&cheese=tasty&test=success">add a querystring</a></li> | |
| <li><a href="javascript: change_url('test', 'failure');">remove test when it equals 'failure'</a></li> | |
| <li><a href="javascript: change_url('test', 'success');">remove test when it equals 'success'</a></li> | |
| <li><a href="javascript: change_url('foo');">remove foo regardless of value</a></li> | |
| </ul> | |
| <div id='results'></div> | |
| <script type='text/javascript'> | |
| function change_url(find, val){ | |
| url = query_string_remove(find, val); | |
| if(url[0]){ | |
| window.location.href = url[1]; | |
| } else { | |
| if(url[3] === '' || url[3] === null || typeof url[3] === 'undefined'){ | |
| text = url[2] + ' was not found.'; | |
| } else { | |
| text = url[2] + '=' + url[3] + ' was not found.'; | |
| } | |
| document.getElementById('results').innerText = text; | |
| } | |
| } | |
| function query_string_remove(find, val){ | |
| qryStr = window.location.search.replace("?", ""); | |
| qryPrts = qryStr.split('&'); | |
| found = false; | |
| split = false; | |
| jQuery.each(qryPrts, function(i, v) { | |
| if(v !== undefined){ | |
| var tmpQry = v.split("="); | |
| if(val === '' || val === null || typeof val === 'undefined'){ | |
| if(tmpQry[0] == find){ | |
| qryPrts.splice(i, 1); | |
| found = true; | |
| } | |
| } else { | |
| split = true; | |
| if(tmpQry[0] == find && tmpQry[1] == val) { | |
| qryPrts.splice(i, 1); | |
| found = true; | |
| } | |
| } | |
| } | |
| }); | |
| if(found){ | |
| qryStr = qryPrts.join('&'); | |
| } | |
| results = [found, ('?'+qryStr), find, val]; | |
| return results; | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment