-
-
Save joakin/4257379 to your computer and use it in GitHub Desktop.
Piece of javascript code: gets the service parameter rom the url (GET) and check if contains something
This file contains 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
/* Fast and easy way of taking a param from the query string */ | |
// From what I gathered, 2 space indentation is prefered in JS | |
// If this is a script included in a page, wrap it in a closure to avoid global | |
// name creation and clashes | |
!function() { | |
/* Named functions on the `root` scope, so that they can be loaded before the | |
* document ready | |
* | |
* Prefer composable abstractions to doing multiple things at once. For | |
* example, we can do getValueFromQueryString(name) and not use globals | |
* inside it. So I'm going to change the name of the func | |
*/ | |
function getValueFromQueryString(query, name) { | |
// No oneliners of more than 2 instructions and never bigger than 80 chars | |
// This code is unreadable | |
// Lets split | |
// Check the docs | |
// https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp | |
// Regex is suposed to be used with new | |
// Also, you know, as in the email I sent you all, when you choose to solve | |
// a problem with regular expressions you have two problems. The regex is | |
// wrong, $ needs to be scaped. | |
var rgx = new RegExp(name + '=' + '(.+?)(&|\$)'), | |
tokens = rgx.exec(query); | |
if (tokens) { | |
// I always used decodeURIComponent and encodeURIComponent, who knows | |
return decodeURIComponent(tokens[1]); | |
} | |
return null; | |
} | |
// Some tests now: (this is for the code review) | |
var queryTest = '?hello=1&name=asd'; | |
console.log('hello val: ' + getValueFromQueryString(queryTest, 'hello')); | |
console.log('name val: ' + getValueFromQueryString(queryTest, 'name')); | |
// Now your shit | |
$(function() { | |
// I like creating locals and then expressing whats going on, like this | |
// (this part is taste and readability mainly) | |
// Also, use on strings always single quotes (JS convention) | |
var serviceVal = getValueFromQueryString(location.search, 'service'), | |
hasCasBullshit = serviceVal.indexOf('spring_cas_security_check') > -1; | |
if (hasCasBullshit) toogleLoginModal(); | |
}); | |
}(); | |
/* | |
* Another way of doing closures (more traditional) | |
* (function() { | |
* // stuff | |
* })(); | |
* | |
*/ | |
// Here you have a uncommented version for copying on the REPL (console) | |
// I removed the jquery.ready stuff | |
// | |
!function() { | |
function getValueFromQueryString(query, name) { | |
var rgx = new RegExp(name + '=' + '(.+?)(&|\$)'), | |
tokens = rgx.exec(query); | |
if (tokens) { | |
return decodeURIComponent(tokens[1]); | |
} | |
return null; | |
} | |
var queryTest = '?hello=1&name=asd'; | |
console.log('hello val: ' + getValueFromQueryString(queryTest, 'hello')); | |
console.log('name val: ' + getValueFromQueryString(queryTest, 'name')); | |
}(); | |
// Here you have how I would do it (no regex). It is longer but I like having | |
// that map parsed properly and also no regex | |
// | |
!function() { | |
// Returns a map with the params of a query string | |
function getQueryParams(query) { | |
var params = query.split('?'); | |
if (!params || params.length < 2) return; | |
var tokens = params[1].split('&'); | |
if (!tokens || tokens.length < 1) return; | |
var map = {}; | |
for(var i = 0, l = tokens.length; i < l; i++) { | |
var token = tokens[i], | |
tokenSplit = token.split('='), | |
hasValue = tokenSplit.length > 1, | |
name = tokenSplit[0], | |
value = (hasValue && decodeURIComponent(tokenSplit[1])) || true; | |
map[name] = value; | |
} | |
return map; | |
} | |
var queryTest = '?hello=1&name=2%20asd'; | |
console.log(getQueryParams(queryTest)); | |
}(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment