Forum Moderators: open

Message Too Old, No Replies

query string to form field - why this javascript wont work

capturing query string and placing it into hidden form field

         

badjo

1:46 pm on Jun 2, 2005 (gmt 0)

10+ Year Member



Hi - I am trying to place a javascript that will capture the url query string and place it into a form field on the webpage so I can record the referring partner ID.

I found this script below but I can not get it to work. Any ideas?

<head>
<!-- Begin
function readID() {
var expDays = 365; // number of days the cookie should last
var expDate = new Date();
expDate.setTime(expDate.getTime() + (24 * 60 * 60 * 1000 * expDays));
var id = GetCookie('id');
if (id == null ¦¦ id == "no id") {
if (location.search.length > 1) id = location.search.substring(1, location.search.length);
else id = "no id";
if (id!= GetCookie('id')) SetCookie('id', id, expDate);
}

// You can change the FORM location below
// where the referral ID is stored on your page
// You then access this element to get the ID
document.idform.id.value = id;

}
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}
function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (argc > 2)? argv[2] : null;
var path = (argc > 3)? argv[3] : null;
var domain = (argc > 4)? argv[4] : null;
var secure = (argc > 5)? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null)? "" : ("; expires=" + expires.toGMTString())) +
((path == null)? "" : ("; path=" + path)) +
((domain == null)? "" : ("; domain=" + domain)) +
((secure == true)? "; secure" : "");
}
// End -->
</script>
</head>

<body OnLoad="readID()">
<form name="idform" action="next" Method="Post">
<input type=text name=id value="">
<input type="submit" name="next" value="Click Here To Enter">
</form>
</body>

Thank you for any comments or ideas. I have been at this for some time and can not get it to work.

ty - badjo

rocknbil

4:26 pm on Jun 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



badjo I don't know why you're using cookies, as it appears to me it's going to set "no id" for the id cookie in any case. But let's simplify. From your post in the other thread:

user comes to site with:
ww.domain.com?id=4444

Note a couple things in my solution:
I've moved the JS below the form. Web pages load from top to bottom; if the form isn't loaded first, you get null or not an object when you try to reference it.

Yes, this can be worked around with body onLoad, but there's a good possibility this can die with a poorly-written popup blocker, so if there's another way, I'll use it. :-D

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>parse Qstring</title>
</head>
<body>
<form name="idform" action="next" Method="Post">
<input type=text" name="id" value="">
<input type="submit" name="next" value="Click Here To Enter">
</form>

<script type="text/javascript">
function getIdFromQstring() {
var url = document.location+''; // Insures string
q=url.split('?');
if (q[1]) {
var pairs = q[1].split('&');
for (i=0;i<pairs.length;i++) {
var keyval = pairs[i].split('=');
if (keyval[0] == 'id') { var v = keyval[1]; break; }
}
}
if (v) { return v; }

}

// Note also this is inline, avoiding onLoad
var id = getIdFromQstring();
if (id) { document.idform.id.value=id; }

</script>

</body>
</html>

badjo

4:48 pm on Jun 2, 2005 (gmt 0)

10+ Year Member



It is hard to express the happiness I get when I pasted your fix in and it worked.

Thank you for the help.

Get a pint on me!