If you have a static HTML page that you need to pass a parameter so that it can be updated easily or work in different instances, this post will show you how to get the parameters via the URL into JavaScript without the use of a backend server.
document.URL contains the entire address bar of the browser, and you can parse this to get the parameters and do something useful with it.
Below is the snippet of code I worked up to do this for a mockup/demo that we were working on for work. Specifically, we wanted to have a single HTML page that would load the same Flex player with different parameters (and wanted to pass those parameters in via the URL) so that we could test with current and future streams/video sources.
<html>
<head>
<script>
function pageLoaded() {
var qAt = document.URL.indexOf("?");
var params = document.URL.substr(qAt+1);
// Use params as needed (parse, or pass along to flash swf):
alert(params); // just echoing in this example.
}
</script>
</head>
<body onload="pageLoaded()">
</body>
</html>
Leave a Reply