codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Help with Form Cookie  jodtoad at 15:26 on Friday, July 16, 2004
 

I'm new to Javascript so I appreciate your help. I have a site that has serveral downloads. The owner of the site wants to collect user information everytime someone downloads. A nuisance if you've already visited and have to fill out a form. I want to place a cookie the first time they download so that the second time the form gets pre-populated for them. I found a site that had a walkthrough on how to do this but my code isn't working. It either isn't writing the cookie, retrieving the cookie or both. Thanks in advance for your help.

Here's the Javascript code I created...

<script type="text/javascript">
//************************
// Set MDF Cookie and File to download
//************************
function Get_Cookie(name) {
var start = document.cookie.indexOf(name+"=");
var len = start+name.length+1;
if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
if (start == -1) return null;
var end = document.cookie.indexOf(";",len);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
document.cookie = name + "=" +escape(value) +
( (expires) ? ";expires=" + expires.toGMTString() : "") +
( (path) ? ";path=" + path : "") +
( (domain) ? ";domain=" + domain : "") +
( (secure) ? ";secure" : "");
}

function setDownloadOption() {
//alert (queryString('file'));
document.downloadForm.file.value = queryString('file');
if (document.downloadForm.file.value.length < 1) document.downloadForm.file.value = 'ims45full';
if (userProfile) getValues(userProfile);
}

function getValues(string) {
getValue(string,"name", document.downloadForm.name, "text");
getValue(string,"company", document.downloadForm.company, "text");
getValue(string,"email", document.downloadForm.email, "text");
getValue(string,"phone", document.downloadForm.phone, "text");
getValue(string,"city", document.downloadForm.city, "text");
getValue(string,"state_region", document.downloadForm.state_region, "text");
getValue(string,"country", document.downloadForm.country, "select");
}

function replace(string,text,by) {
// Replaces text with by in string
var i = string.indexOf(text);
var newstr = '';
if ((!i) || (i == -1)) return string;
newstr += string.substring(0,i) + by;

if (i+text.length < string.length)
newstr += replace(string.substring(i+text.length,string.length),text,by);

return newstr;
}

function getValue(string,elementName,object,elementType) {
// gets value of elementName from string and populates object of elementType

var startPos = string.indexOf(elementName + "=")

if (startPos > -1) {
startPos = startPos + elementName.length + 1;
var endPos = string.indexOf("&",startPos);
if (endPos == -1) endPos = string.length;

var elementValue = unescape(string.substring(startPos,endPos));

if (elementType == "text") object.value = elementValue;
if (elementType == "password") object.value = elementValue;
if (elementType == "select") object.selectedIndex = elementValue;
if (elementType == "checkbox") object.checked = onCheck(elementValue);
if (elementType == "radio") object[elementValue].checked = true;
}
}
</script>


The body onload looks like this...
<body onload="setDownloadOption();">

Thanks
jodtoad








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Testing file for type of data
•  Running VB6 application as a ASP.NET application
•  login system with asp.net
•  Re: Help: Trouble with z-Index and SELECT lists
•  What is wrong with this ASP codes? It don`t seem to work???
•  Need help with ListBox control
•  Re: Good Javascript/DHTML Reference
•  Re: scrolling table
•  Re: simulation of road traffic


Recent Articles
Communicating with the Database (Using ADO)
MagicGrid
Simple Thumbnail Browsing Solution
Type Anywhere
A Better Moustrap: FmtDate to replace FormatDateTime
ASP.NET Forum Source Code
Internal Search Engine
Javascript Growing Window
Simple date validation
Search engine friendly URLs using ASP.NET (C#.NET)


Site Survey
Help us serve you better. Take a five minute survey. Click here!

© Copyright codetoad.com 2001-2004