Skip to content

Instantly share code, notes, and snippets.

@jauco
Created June 12, 2010 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jauco/435668 to your computer and use it in GitHub Desktop.
Save jauco/435668 to your computer and use it in GitHub Desktop.
Jquery plugin for passing paramaters to scripts without dirtying the global scope
//Jquery plugin to for passing paramaters to scripts without dirtying the global scope
"use strict";
(function createGetScriptParameters($) {
$.getScriptParameters = function getParams() {
//From the comments on http://bit.ly/dw5eYp
var scripts = document.getElementsByTagName("script"),
thisScript = scripts[scripts.length - 1], //Get the current script tag
paramString = "{" + thisScript.innerHTML.replace(/[\r\n]+\s*\/\//g, '') + "}";
return $.parseJSON(paramString);
};
}(jQuery));
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="params.js"></script>
<script src="test.js">
//"param1" : "blah",
//"param2" : {"foo":3}
</script>
<script src="test.js">
//"param1" : "blie",
//"param2" : {"fab":4}
</script>
</head>
<body><p></p></body>
</html>
"use strict";
(function ($) {
var params = $.getScriptParameters();
$(document).ready(function () {
$("<p>").text("a: " + JSON.stringify(params)).appendTo($("body"));
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment