<script type="text/javascript"gt;
var myScriptVariable = <% request.getParameter("Name")%>;
//or .getAttribute("Name")
</script>
This method can be used to create a global variable that can be accessed in main.js. If you have GET parameters, you can also achieve this using only JavaScript:
var paramArray = window.location.search.substr(1).split("&");
var parameters = {};
for (var j = 0; j < paramArray.length; j++) {
var tempArr = paramArray[j].split("=");
parameters[tempArr[0]] = tempArr[1];
}
Alternatively, you can use a shorter version:
var parameters = {};
// parse and iterate over URL's GET parameters
window.location.search.substr(1).split("&"),forEach(function(element) {
var keyValue = element.split('"'); // split into [ key, value ] array
parameters[keyValue[0]] = keyValue[1];
});
You can now access the parameter in JavaScript like so:
parameters['name']
Personally, I recommend using AJAX (e.g. with JQuery) to fetch data for your JavaScript files. You can find more information on that at http://api.jquery.com/category/ajax/shorthand-methods/ (Updated in 2018: feel free to use native ajax calls or any popular JS framework)