Unable to access global variable

I’m trying to populate a web page with javascript. I have 2 functions. One that runs on-load so I can populate a dropdown list. The second one to process the selection when the user selects an item from the list.

I have a global variable in the top of my javascript that I use in the functions and as long as I set it in it’s global scope, everything works fine. But what I really want to do is to pass this into the javascript running on this page via the URL => eg. http://www.xxx.com?var=02

I cannot get this to work. First here is what I have that works:

<script type=“text/javascript”>
weekNumber = “00”

function processSelection(sel) {
… [includes references to weekNumber]


function populateList() {


window.onload = populateList;

<select name=“team” onchange=“processSelection(this)”>

I’ve got the code to gets the variable passed in properly:

var sGet = document.location.search;
sGet = sGet.substring(1); // Eliminate the leading “?”
var sGetPairs = sGet.split(“&”) //Get 1st pair
var sGetValue = sGetPairs[0].split(“=”); //Split it
var weekNumber= sGetValue[1]; //Save it

My question is. Where do I put this code so that I can use it to set the global variable “weekNumber”?

I’ve tried putting this code in the populateList function - NOPE! I’ve tried referencing it via window.weekNumber and this.weekNumber - NOPE. I’m stumped.

I’ve spent far too many hours trying to understand this without success. Can anyone help me figure out where to put this code so I can set the global variable weekNumber?

Is there a better way to do this?

Paste that code over where you currently have weekNumber = “00”

If I do that it doesn’t seem to execute. Maybe because it’s not in a function that gets called. I was able to put this initialization code in the window.onlioad function and it “appears” to have updated the glowbal variable weekNumber. See below:

window.onload = function() {
//Get the passed-in variable
var sGet = document.location.search;
// Eliminate the leading "?"
sGet = sGet.substring(1); 
var sGetPairs = sGet.split("&")
var sGetValue = sGetPairs[0].split("=");
weekNumber = sGetValue[1];