Last active
January 3, 2016 05:29
-
-
Save dun4n/8415917 to your computer and use it in GitHub Desktop.
Retrieve a JSON config object from URI parameter
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
var config = (function() { | |
var s = document.getElementsByTagName('script'); | |
var uri = Array.prototype.slice.call(s).slice(-1)[0].src.match(/\?.*/g) | |
return uri.length > 0 ? JSON.parse('{"' + decodeURI(uri[0].substr(1)).replace(/"/g, '\\"').replace(/&/g, '","').replace(/\=/g,'":"') + '"}') : {} | |
})() | |
/* | |
<script type="text/javascript" src="script.js?foo=bar"></script> | |
config -> {"foo": "bar"} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment