...

View Full Version : Passing data to HTML



Tony Davis
01-11-2006, 07:03 PM
I have this code that seems to work when I want to pass a URL or other type of data to HTML. I also found some examples on Huntingground (see http://www.huntingground.freeserve.co.uk/main/mainfram.htm) that explain how to do this. How do I go about parsing a SECOND (or more) value? I would like to pass more than just the one string.

For example, how do I parse this string:
http://www.mywebsite.com/testing?id=testing&count=22&return=confirm.html

var urlstring = location.href;
var qrystring = urlstring.substring(urlstring.indexOf("=") + 1, urlstring.length);
document.write(qrystring);

<script type="text/javascript">
<!--
var dataPassed = '';
if (location.search.length > 0)
dataPassed = unescape(location.search.substring(1));
document.write(dataPassed);
//-->
</script>

Thanks,
-tdavis

rlemon
01-11-2006, 08:54 PM
I have this code that seems to work when I want to pass a URL or other type of data to HTML. I also found some examples on Huntingground (see http://www.huntingground.freeserve.co.uk/main/mainfram.htm) that explain how to do this. How do I go about parsing a SECOND (or more) value? I would like to pass more than just the one string.

For example, how do I parse this string:
http://www.mywebsite.com/testing?id=testing&count=22&return=confirm.html

var urlstring = location.href;
var qrystring = urlstring.substring(urlstring.indexOf("=") + 1, urlstring.length);
document.write(qrystring);

<script type="text/javascript">
<!--
var dataPassed = '';
if (location.search.length > 0)
dataPassed = unescape(location.search.substring(1));
document.write(dataPassed);
//-->
</script>

Thanks,
-tdavis

i'm not really sure what your asking for here?

do you need a function to simply parse a url string to pull out the variables?

i.e

parsing

myDomain.com/?variable1=test&variable2=1234

you want to return

variable1 = test
variable2 = 1234

in an array i assume?? maybe just as variables??

so like

var rs1 = test;
var rs2 = 1234;
*not that clean - you will obviously have to store it into the variable.

Tony Davis
01-11-2006, 09:06 PM
Yes. that is what i am trying to do, parse the string:
here is the code I am using now. It works, but only parses the first parm:

as in: E:\slideshow\index.htm?id=gila&count=22

alert(GetParam("id")); //this shows gila&count=22
alert(GetParam("count")); //this is blank


<SCRIPT LANGUAGE="Javascript">
function GetParam(name)
{
var start=location.search.indexOf("?"+name+"=");
if (start<0) start=location.search.indexOf("&"+name+"=");
if (start<0) return '';
start += name.length+2;
var end=location.search.indexOf("&",start)-1;
if (end<0) end=location.search.length;
var result=location.search.substring(start,end);
var result='';
for(var i=start;i<=end;i++) {
var c=location.search.charAt(i);
result=result+(c=='+'?' ':c);
}
return unescape(result);
}
</SCRIPT>

Nischumacher
01-11-2006, 09:11 PM
<script>
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i=0;i<vars.length;i++) {
var pair = vars[i].split("=");
if (pair[0]==variable) document.write(variable + ' = ' + pair[1]);
}
}
}
</script>
not tested

vwphillips
01-11-2006, 09:12 PM
may be missing something

but
I would normally

myDomain.com/?est&234


var variable1,variable2,variable3;
var data=self.location.toString().split('?');

if (data[1]){

variable1=data[1].split('&')[0];
variable2=data[1].split('&')[1];
// or
variable3=data[1];
}

rlemon
01-11-2006, 09:26 PM
there are numerous examples in the Post a Javascript form.

here is a good one posted by another user in said form




function urlDecode(s)
{
return unescape(s.replace("+", " "));
}

function parseQueryString()
{
var obj = new Object();

var nvpairs = location.search.substring(1).split("&");

for (var idx = 0; idx < nvpairs.length; idx++)
{
var tokens = nvpairs[idx].split("=");

obj[urlDecode(tokens[0])] = tokens.length == 2 ?
urlDecode(tokens[1]) : undefined;
}

return obj;
}



however if a server side lang is avail to do this i suggest using it.

for example: here is it in php

http://codingforums.com/showthread.php?t=72151

Nischumacher
01-11-2006, 09:29 PM
myDomain.com/?est&234
var variable1,variable2,variable3;
var data=self.location.toString().split('?');
if (data[1]){
variable1=data[1].split('&')[0];
variable2=data[1].split('&')[1];
// or
variable3=data[1];
}
this is a better method...

TONY : you got to use "E:\slideshow\index.htm?gila&22"

var id, count;
var data=self.location.toString().split('?');
if (data[1]){
id=data[1].split('&')[0]; // will assign gila
count=data[1].split('&')[1];// will assign 22

Tony Davis
01-11-2006, 09:42 PM
I get the same results.

And dont i have to have the = signs?
The first split takes the string to the end, doesnt it?

Tony Davis
01-11-2006, 10:21 PM
I found the problem. But I cant believe it! I created an HTML link as follows, and now it works. I originally included the path to the script (as in the first example) then when I removed that (as in the second) it works. Why?

<a href="e:\slideshow\index.htm?id=gila&count=22">Test</a>

<a href="index.htm?id=gila&count=22">Test</a>

rlemon
01-11-2006, 10:22 PM
doesn't like the FS??

i have had problems trying to run some scripts on local machine.

maybe it only likes relative paths?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum