Skip to content

Instantly share code, notes, and snippets.

@dun4n
Last active January 3, 2016 05:29
Show Gist options
  • Save dun4n/8415917 to your computer and use it in GitHub Desktop.
Save dun4n/8415917 to your computer and use it in GitHub Desktop.
Retrieve a JSON config object from URI parameter
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