The window.location.search
property returns the part of the URL that follows the ?
symbol, including the ?
symbol.
So for example it might return ?paramname=paramvalue
. When you call substring(1)
on it you get paramname=paramvalue
which is what gets passed to the document.getElementById
function which obviously is wrong because such element does doesn't exist on your DOM.
You could use the following javascript function
to read query string parameter values:
function onLoad() {
var divname = getParameterByName('divname');
document.getElementById(divname).style.display = 'block';
}
This assumes that you have a query string parameter name called divname
:
?divname=some_div_name
Adjust the parameter passed to the getParameterByName
function if your query string parameter is called differently.
You might also want to introduce error checking into your code to make it more robust:
function onLoad() {
var divname = getParameterByName('divname');
var divElement = document.getElementById(divname);
if (divElement != null) {
divElement.style.display = 'block';
} else {
alert('Unable to find an element with name = ' + divname);
}
}