View Full Version : HTML Parameter passing
Milojko22
04-29-2004, 10:54 AM
Hi,
Is there a way to pass a string from one HTML file to another.
I would like to have one HTML file, i.e. picture.html in which I would then pass the name or location of the image I want to have displayed.
Can this be done?
Thanks.
agent002
04-29-2004, 11:03 AM
Hi Milojko, and welcome to HTMLForums! :)
You could use a query string plus a server side script or JavaScript to pass data... or alternatively cookies, but I'd recommend a query string.
You can have this link on page 1:
<a href="page2.html?firstname=John&lastname=Doe">View page 2</a>
And then this code for page 2:
<html>
<head>
<title>Page 2</title>
<script type="text/javascript">
var query = location.href.substring((location.href.indexOf('?')+1), location.href.length);
if(location.href.indexOf('?') < 0) query = '';
querysplit = query.split('&');
query = new Array();
for(var i = 0; i < querysplit.length; i++){
var namevalue = querysplit[i].split('=');
namevalue[1] = namevalue[1].replace(/\+/g, ' ');
query[namevalue[0]] = unescape(namevalue[1]);
}
window.onload = function(){
// Do stuff with query string data here.
document.getElementById('firstname').innerHTML = query['firstname'];
document.getElementById('lastname').innerHTML = query['lastname'];
}
</script>
</head>
<body>
<h1>Page 2</h1>
<p>Your name is <span id="firstname"></span> <span id="lastname"></span></p>
</body>
</html>
You can change the command at the location of "do stuff with query string data here", if you get further questions, just ask :)
Milojko22
04-29-2004, 02:59 PM
:rocker:
OLA!
I'll say this: YOU THE MAN!!!:D
THANKS!!!
This is all I actually needed but I wouldnt do it without your help:
<HTML>
<HEAD>
<TITLE>Picture</TITLE>
<SCRIPT TYPE="text/javascript">
function getPicture()
{
var picture = location.href.substring((location.href.indexOf('pic=')+4), location.href.length);
return picture;
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT TYPE="text/javascript">
document.write("<IMG SRC='"+getPicture()+"'>")
</SCRIPT>
</BODY>
</HTML>
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.