-
-
Save jalama/a4ac33af70982bac539d2b9466720b1d to your computer and use it in GitHub Desktop.
Add Drupal.arg() for parsing urls in Drupal
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
/** | |
* Adds method arg to the Drupal object for grabbing url args | |
* | |
* @author Aaron Klump, In the Loft Studios, LLC | |
* @see http://www.intheloftstudios.com | |
* @see http://gist.github.com/3078482 | |
* | |
* For use in a theme or module add the following to your .info file | |
* @code | |
* scripts[] = [path to js dir]/drupal.arg.js | |
* @endcode | |
* | |
* @author Aaron Klump, In the Loft Studios, LLC | |
* @see http://www.intheloftstudios.com | |
* @see http://gist.github.com/3078482 | |
*/ | |
/** | |
* JS equivalent of arg() | |
* | |
* @param index | |
* @param path | |
* | |
* @return array, string or false | |
*/ | |
var Drupal = Drupal || { 'settings': {}, 'behaviors': {}, 'locale': {} }; | |
Drupal.arg = Drupal.arg || function (index, path) { | |
if (path === null) { | |
path = window.location.pathname; | |
} | |
if (path.substr(0, 1) === '/') { | |
path = path.substr(1); | |
} | |
path = path.split('?'); | |
var args = path[0].split('/'); | |
if (index === null) { | |
return args; | |
} | |
if (args[index]) { | |
return args[index]; | |
} | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment