About Randy L. Taylor Expertise I can answer advanced questions concerning most aspects of web programming including HTML, DHTML, DOM, JavaScript, Cookies, Internet Explorer, and CSSs. I can answer basic questions concerning XML and ActiveX. I can also answer questions concerning Regular Expressions, logical design algorithms, and Object Oriented Programming.
Experience
Past/Present clients Upp Business Systems, LLC
Headquarters Standard Systems Group, USAF
I know it is possible with ASP but need to keep the HTML page.
Any thoughts?
Regards
Rob
Answer Hi there Rob,
To be honest, I've never tried anything like that before. But in theory, it should be possible. :)
The URL is stored in the location object's HREF property. So, by parsing the URL, you should be able to mimic the variable creation that an ASP page does.
Here is a little test HTML page I wrote. If you save it to your hard drive, open it up in IE, you'll probably get an error when it tries to alert(myURLVal). But, if you go to the Address bar and add "?myURLVal=worked" to the end of the URL, then press "Go" - you should see the 'worked' value alerted.
I included comments so I think you can see what's going on. Obviously it's not perfect (doesn't change %20 to spaces, only works for one name-value pair, etc...), but it gives you an idea of how to do something like what you want - with a little more time and effort you should have it working in no time.
take care,
Randy L. Taylor
<!--when the page loads, parseURLForVariables() will be called-->
<html>
<body onload="parseURLForVariables()">
</body>
</html>
<script language="Javascript">
function parseURLForVariables(){
var URLString = location.href;
//find the "?" in the URL
var questionMarkIndex = URLString.indexOf("?");
//find the next "=" index after the "?"
var equalSignIndex = URLString.indexOf("=", questionMarkIndex);
//save off the variable name
var URLVarName = URLString.substring(questionMarkIndex+1, equalSignIndex);
//save off the variable value
var URLVarValue = URLString.substring(equalSignIndex+1, URLString.length);
//use the eval function to create a variable named URLVarName
//that has a value equal to URLVarValue
eval("var " + URLVarName + " = '" + URLVarValue + "';");
//now try and alert the var
//if the URL contains '?myURLVal=worked' then we
//should see 'worked' alerted
try{
alert(myURLVal);
}
catch(e){
}
}