/    Sign up×
Community /Pin to ProfileBookmark

Need help with Cookie script

Hi.

I need a cookie script to set a cookie of a referring site.

People visit our site and click on a link that takes them to our shopping cart hosted on another site. I am running a script for that link on our site that adds a query string to the URL so when it lands on that first shopping cart page, the URL showing in the address bar is something like ourshoppingcartpage.com/?q=xxxxx. This tells me where the visitor originated from. I want to put the value of this url in a cookie. Then when people register, that same cookie value will be part of the form results.

I can’t use php because I don’t have access to the shopping cart code but I can use Javascript. The problem is I don’t know much about Javascript.

Please help. Thanks

to post a comment
JavaScript

16 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineApr 27.2010 — Hi.

I need a cookie script to set a cookie of a referring site.

[/QUOTE]
This example will store the value of ?q=xxx in a session cookie called 'refSite':

[CODE]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Store q= param in cookie</title>
</head>

<body>

<form id="myForm" action='#' method='get'>
<p>
<input name='myField' type='text'>
</form>

<script type='text/javascript'>

var v = location.search.slice(1).match( /(^|&)q=([^&]+)/ );

if( v )
{
document.cookie = 'refSite=' + v[2];

document.getElementById('myForm').myField.value = decodeURI( v[2] );
}

</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@Sales_TrainiauthorApr 27.2010 — Please explain why you have a form and a form field in there. I'm not asking my visitors to enter anything into a form.

Thanks
Copy linkTweet thisAlerts:
@Sterling_IsfineApr 27.2010 — Please explain why you have a form and a form field in there. I'm not asking my visitors to enter anything into a form.

Thanks[/QUOTE]

You said you wanted the stored value to become part of the form results, so I demonstrated how to do that.
Copy linkTweet thisAlerts:
@Sales_TrainiauthorApr 27.2010 — this is what I wrote:

"the URL showing in the address bar is something like ourshoppingcartpage.com/?q=xxxxx. This tells me where the visitor originated from. I want to put the value of this url in a cookie"

The value of the URL must first be placed in the cookie as soon as they land on the first shopping cart page. This has to be done automatically.

I need a script to store the value in a cookie and have that cookie never change where ever they go on the site.

Then later when they check out, that stored value will then be put in the form.

I think you gave me the second part-right?

Thanks for your help
Copy linkTweet thisAlerts:
@Sterling_IsfineApr 28.2010 — 
I think you gave me the second part-right?

[/QUOTE]
I gave you both parts.
Copy linkTweet thisAlerts:
@Sales_TrainiauthorApr 28.2010 — I understand now. Thanks

One additional problem I'm having. I don't have access to this shopping cart code and it must use some other javascript to access the input fields. When I open up the source code of the form page, I can see the name of my form (editform), and my custom input field. I can't custom name it, the system does. I can only give it a class. The class I gave it is "urlfield". Then the system gave it the class of "urlfield FormField", gave it the id and the name. This is what I get.

[CODE]<input type="text" size="30" class="urlfield FormField" id="FormField_33" name="FormField[3][33]" value="" />[/CODE]
So I've tried:

[CODE]<script type='text/javascript'>
var v = location.search.slice(1).match( /(^|&)q=([^&]+)/ );
if( v )
{
document.cookie = 'refSite=' + v[2];
document.getElementById('editform').FormField[3][33].value = decodeURI( v[2] );
}
</script>[/CODE]

But it doesn't work.

I also tried:

document.getElementById('editform')["FormField[3][33]"].value = decodeURI( v[2]

have any suggestions?

Thanks for your help
Copy linkTweet thisAlerts:
@SpinnerApr 28.2010 — Can you go right to the input-field like this, maybe?

document.getElementById('FormField_33').value = decodeURI( v[2] );
Copy linkTweet thisAlerts:
@Sterling_IsfineApr 28.2010 — You must run the script at a point below the form.

If the form's ID (not name) is 'editform' (case must match exactly) then[CODE]document.getElementById('editform')["FormField[3][33]"].value[/CODE]should work.

If the form's [B]name[/B] is 'editform', you can use:[CODE]document.forms.editform["FormField[3][33]"].value[/CODE]What error message do you get in the FireFox error console?
Copy linkTweet thisAlerts:
@Sales_TrainiauthorApr 28.2010 — Nope

Doesn't work. if you go to https://store-ce289.mybigcommerce.com/checkout.php?tk=b4fc3abecb6c2555f66ddab5c9f72148

You can check out the source code and see what I mean. I have all the cookie scripts in the head section which is an include file for all pages. I'm trying to put a cookie value into a custom input field in the form of this page. Look for id="FormField_33" or name="FormField[3][33].

Thanks
Copy linkTweet thisAlerts:
@SpinnerApr 28.2010 — function putcookie(); {

var v = location.search.slice(1).match( /(^|&)q=([^&]+)/ );

if( v ){

document.cookie = 'Referredby=' + v[2];

document.getElementById('FormField_33').value = decodeURI( v[2] );

}

}

If you look at the first line there, I think you might want to lose the ;
Copy linkTweet thisAlerts:
@SpinnerApr 28.2010 — Since you use jQuery on the site already though, I'd move the execution of the scripts to after the dom-tree is ready.

$(document).ready(function(){

}); // End document.ready function


Also, I am unsure about your syntax here:

putcookie();

else

setCookie();

Is that valid javascript? According the Firebug, it's not ?
Copy linkTweet thisAlerts:
@Sales_TrainiauthorApr 28.2010 — I don't know javascript. I've just tried to make things work as best I can from forums and reading. So you tell me. I want to see if a cookie is there called "Referredby" if no cookie then setCookie
Copy linkTweet thisAlerts:
@SpinnerApr 28.2010 — Ok, you are a little confused here. I'll try to explain better and supply exactly what I think you want.

We will skip the set/put cookie functions and make two different ones that might be easier to understand and use.

[CODE]<script type='text/javascript'>

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}


$(document).ready(function(){
var existingCookie=readCookie('Referredby');
if (existingCookie) {
// The cookie exists, just put the data into the formfield as you wanted
document.getElementById('FormField_33').value=existingCookie;
}
else {
// The cookie does not exist.
// We will create the cookie if there is a section of the location matching q=something
// If there is no such part of the location, no cookie will be set.
var v = location.search.slice(1).match( /(^|&)q=([^&]+)/ );
if (v){
// We have a valid part in the location, set the cookie
createCookie('refSite',v[2],365);
document.getElementById('FormField_33').value = decodeURI(v[2]);
}
}
}); // End document.ready function

</script>[/CODE]
Copy linkTweet thisAlerts:
@Sterling_IsfineApr 28.2010 — I have all the cookie scripts in the head section which is an include file for all pages.[/QUOTE]Well I can't find the code I gave you, but if it's anywhere in the head it won't work - like I said previously it must be placed below the form.

Another trivial problem is that your page has a grand total of two forms sharing the name 'editform', opening at lines 304 and 848, which isn't going to help matters.
Copy linkTweet thisAlerts:
@SpinnerApr 29.2010 — No disrespect, but if you can not solve this with my previous post, there isn't hope. Sorry.

Then I can only recommend that you hire a consultant to modify your source for you.

That script does EVERYTHING you wanted it to, and can replace any and all code you had previously when it came to these cookies.

Good luck.
Copy linkTweet thisAlerts:
@Sales_TrainiauthorMay 13.2010 — Well I finally got the thing to work. But one more thing.

The cookie code in the above post has a line in it that needs to be fixed:

[CODE]var v = location.search.slice(1).match( /(^|&)q=([^&]+)/ );[/CODE]

This code is picking up everything after the "?q" in the URL but then leaves valuable info out.

This is what's happening. When people come to my site, a session is started. If they come from a search, I have a script that adds the search string to the end of the URL that goes to my shopping cart where this cookie is. So the URL becomes something like myshoppingcart.com/category/?q=http://www.google.com/url?sa=t&source=web&blahblahblah. But the cookie only picks up the google.com/url?sa=t

I need the cookie to pick up everything.

Can someone fix the code above so it will do that?

Thanks for all your help.
×

Success!

Help @Sales_Traini spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...