Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieve data from url 3

Status
Not open for further replies.

vgeng

Technical User
Feb 5, 2003
15
BE
Using the following URL and code I get always the 'message' that firstname = undefined

my url equals : file:///H:/test2.html?concert=ACDC

my code

<HTML>
<HEAD>
<SCRIPT LANGUAGE="JavaScript">
function getParams() {
var idx = document.URL.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs.split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}
params = getParams();

</script>
<TITLE>test2</TITLE>
</HEAD>
<Body>
<SCRIPT LANGUAGE="JavaScript">
firstname = unescape(params["concert"]);
document.write("firstname = " + firstname + "<br>");
</script>
</body>
</HTML>



Thanks for your help

Geert
 
Hi

Well, it works for me in FireFox.

If you test it with Explorer, note that I found it has problems handling the query string with file:// protocol. Try to test it through a web server.

In meantime a suggestion anyway :
Code:
[red]var[/red] params = getParams();

Feherke.
 
I find it annoying to see scripting something like that to start with. Here is how it can be done.
[tt]
<HTML>
<HEAD>
<TITLE>test2</TITLE>
</HEAD>
<Body>
<div id="divid"></div>
<SCRIPT LANGUAGE="JavaScript">
function getParams() {
var idx = window.location.search.indexOf('?');
var params = new Array();
if (idx != -1) {
var pairs = window.location.search.substring(idx+1, window.location.search.length).split('&');
for (var i=0; i<pairs.length; i++) {
nameVal = pairs.split('=');
params[nameVal[0]] = nameVal[1];
}
}
return params;
}

//var params=new Array();
window.onload=function() {
params = getParams();
firstname = unescape(params["concert"]);
/*
document.open();
document.write("firstname = " + firstname + "<br>");
document.close();
*/
//preferrably
document.getElementById("divid").innerHTML="firstname = " + firstname ;
}
</SCRIPT>
</Body>
</HTML>
[/tt]
 
Explorer was the problem
It works fine in FireFox and on an virtual server

Thanks Feherke
and thanks tsuji for your alternitive
 
I don't think you understand the necessity of an "alternative". If it is just an alternative, I wouldn't have bothered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top