Created
June 12, 2010 12:02
-
-
Save jauco/435668 to your computer and use it in GitHub Desktop.
Jquery plugin for passing paramaters to scripts without dirtying the global scope
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
//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)); |
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
<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> |
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
"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