AllExperts > Javascript 
Search      
Javascript
Volunteer
Answers to thousands of questions
 Home · More Javascript Questions · Answer Library  · Encyclopedia ·
More Javascript Answers
Question Library

Ask a question about Javascript
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
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

 
   

You are here:  Experts > Computing/Technology > Focus on JavaScript > Javascript > Querystring.Request using JavaScript or VBScript on an HTML page

Javascript - Querystring.Request using JavaScript or VBScript on an HTML page


Expert: Randy L. Taylor - 4/2/2004

Question
Hello,

Is it possible to request a variable from an HTML page URL using JavaScript or VBScript.  The URL is: http://mmedia.glos.ac.uk/students/mu305/soe/Andalucia/insert.htm?imageFile=Image...

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){
 }
}  

Add to this Answer   Ask a Question


 
User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2008 About, Inc. AllExperts, AllExperts.com, and About.com are registered trademarks of About, Inc. All rights reserved.