JS help please

jlknauff

New member
Aug 25, 2008
237
1
0
OK guys, this should be a simple one, but I know next to nothing about javascript...

I am using the following code to write a cookie
Code:
<script type="text/javascript">

    function setCookie(){
    var cookieName="UserName";
    var cookieValue="Duminda";
    var expiryDate = new Date();
    var oneDayFromNow = expiryDate.getTime() + (24 * 60 * 60 * 1000);
    expiryDate.setTime(oneDayFromNow);
    document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expiryDate.toString()+";";
    return true;

</script>

And then the following to get/write it into a document

Code:
<script type="text/javascript"> document.write(getCookie('UserName')); </script>

It isn't returning anything though. What am I doing wrong?
 


No closing bracket in your function, and function never called. Or perhaps you didn't paste everything...
 
Hmmm...I'm assuming this is what you meant...I tried it like this, but I still get nothing:

Code:
    function setCookie(){
    var cookieName="UserName";
    var cookieValue="Duminda";
    var expiryDate = new Date();
    var oneDayFromNow = expiryDate.getTime() + (24 * 60 * 60 * 1000);
    expiryDate.setTime(oneDayFromNow);
    document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expiryDate.toString()+";";
    return true;
    }
 
1. Having a setCookie() function alone won't do you anything unless there is code elsewhere in the page that actually calls the function. Is there an "onload=setCookie()" line anywhere, or "window.onload = setCookie()" maybe?

2. You have a setCookie() function, but do you also have a getCookie() function? Your document.write line is trying to output the response to that, but you haven't indicated that it actually exists.
 
  • Like
Reactions: jlknauff
OK, here is what I currently have:

Code:
<script type="text/javascript">

    function setCookie(){
    var cookieName="UserName";
    var cookieValue="Duminda";
    var expiryDate = new Date();
    var oneDayFromNow = expiryDate.getTime() + (24 * 60 * 60 * 1000);
    expiryDate.setTime(oneDayFromNow);
    document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expiryDate.toString()+";";
    return true;
    }

</script>

</head>

<body class="page page-id-3246 page-parent page-child parent-pageid-105 page-template page-template-franchisee-page-php" onload=setCookie()>

<script type="text/javascript">
    function getCookie(UserName){
    document.write(getCookie("UserName"));
    }
</script>

I have no idea what I'm doing wrong, like I said, I know next to nothing about js. This is very frustrating. :angryfire:
 
I have no idea what I'm doing wrong, like I said, I know next to nothing about js. This is very frustrating. :angryfire:

It's also frustrating to us as well because you don't know what the fuck you're doing. Maybe you should pay someone to handle this because you don't even have anything close to what will work. Sure I could tell you why you're an idiot but that just wont do it justice, besides it'll be over your head.
 
You're calling the GetCookie function recursively within itself. Heh.

Try replacing it with this:
Code:
    function getCookie(thearg){
       var results = document.cookie.match('(^|;) ?'+thearg
          +'=([^;]*)(;|$)' );
       if (results) {
           return (unescape(results[2]));
       } else {
           return null;
       }
    }
 
Hey Rage9, if it's so frustrating for you to read this, then fuck off and don't read it you whiny little bitch.

The purpose of this forum is for people to ask questions. You didn't magically wake up one day and all of the sudden know everything you know. Now stick your mothers dildo back in your pie hole and shut the fuck up.
 
Code:
<script type="text/javascript">

    function setCookie(){
    var cookieName="UserName";
    var cookieValue="Duminda";
    var expiryDate = new Date();
    var oneDayFromNow = expiryDate.getTime() + (24 * 60 * 60 * 1000);
    expiryDate.setTime(oneDayFromNow);
    document.cookie = cookieName+"="+escape(cookieValue)+";expires="+expiryDate.toString()+";";
    return true;
}
</script>

The problem is you simple created a function called setCookie, BUT never actually tell it to run. Talking in a vacuum, the code should look like this:

Code:
<script type="text/javascript">

    function setCookie(){
blah blah code
    }

    setCookie;

</script>
The last line actually runs the function... Again talking from within a vacuum.

--

Let's take a look at the other errors you are committing from a programming standpoint. ASSUMING the javascript code setCookie is correct: you also have a call to a script to write the cookie's content's username. What guarantee do you have that the writing of the username will occur before the setting of the cookie? None.

Sometimes a browser will load ALL the elements first, and see the write element, run it, before the setcookie. (Again this is talking from a vacuum, since I have no idea what other scripts are running, or what this is even used for).

So, if you even have the script working properly, and testing it 5 or 6 times, it doesn't mean, that is will run 100% of the time in that order, since there is no hierarchy order of the program. onload, or (document).ready, are utilized to solve this problem.

Check out this link to get started with an understanding: JavaScript Cookies

For your specific example, use this link:
JavaScript and Cookies

Notice, how the script waits for an input to run, or some action... If you want to automatically have this all run on loading, set the cookie, and in the same function, write the cookie to a document element.

Good luck bro.
 
  • Like
Reactions: jlknauff
OK CCarter, based on the info you provided, I was able to get it working on that page, but when you go to another page and try to retrieve the cookie, you get a value of null.

Here's what I have, do you see anything wrong with it?

Code:
<!--
function setCookie()
{
    cookievalue= "<?php echo get_post_meta($post->ID, 'phone', true); ?>" + ";";
    document.cookie="localnumber=" + cookievalue;
}
setCookie();

    function getCookie(name)
{    
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null; 
}
getCookie();
//-->
 
OK CCarter, based on the info you provided, I was able to get it working on that page, but when you go to another page and try to retrieve the cookie, you get a value of null.

Here's what I have, do you see anything wrong with it?

Code:
<!--
function setCookie()
{
    cookievalue= "<?php echo get_post_meta($post->ID, 'phone', true); ?>" + ";";
    document.cookie="localnumber=" + cookievalue;
}
setCookie();

    function getCookie(name)
{    
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null; 
}
getCookie();
//-->


I am going to assuming a couple of things here - Again talking in a vacuum since I don't know all the factors of the page - On page 1, you set the cookie, and then asked to retrieve the same cookie. Then on page 2, you simply, retrieved the cookie, and NOT attempted to set the cookie again. (You only show the page one piece of code - hopefully - cause if you are using this as page 2, you are in trouble).

Second, and this is critical, in your getCookie function, you specify a "name" variable, but when you call the getCookie, you do not specify the same variable, and leave it null/void/blank.

A critical thing to understand about cookies, is they are stubborn. If you create a cookie, which has the domain as "www.yourwebsite.com", and attempt to call it from "yourwebsite.com", it will not work. When setting the cookie, you should use the domain as .yourwebsite.com, that way both the www, non www, and subdomains can interact with that cookie.

Also, how do you know that the cookie is really set? I use the Firefox plugin "Web Developer" (currently 1.2.1 version), to help me figure things out, when a cookie is set, the domain name, and the information that is set.

If you would like, I can take a look at your code, program, and see where the mistake is occurring. PM if you are interested. The guides I gave should have solved all the problems, but it sounds like there is something outside the vacuum that may be occurring.
 
On page 1, you set the cookie, and then asked to retrieve the same cookie. Then on page 2, you simply, retrieved the cookie, and NOT attempted to set the cookie again. (You only show the page one piece of code - hopefully - cause if you are using this as page 2, you are in trouble).

Right, the cookie is set on the initial page, but not on the other pages. For those, it simply has:

Code:
<script type="text/javascript">

<!--
    function getCookie(name)
{    
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null; 
}
getCookie();
//-->

</script>

Second, and this is critical, in your getCookie function, you specify a "name" variable, but when you call the getCookie, you do not specify the same variable, and leave it null/void/blank.

I'm not sure the solution here, originally, I tried using

Code:
function getCookie("localnumber")

But that killed the script entirely.

A critical thing to understand about cookies, is they are stubborn. If you create a cookie, which has the domain as "www.yourwebsite.com", and attempt to call it from "yourwebsite.com", it will not work. When setting the cookie, you should use the domain as .yourwebsite.com, that way both the www, non www, and subdomains can interact with that cookie.

I noticed in some tutorials, they didn't use the domain while others did. I omitted it so there would be less to trouble shoot for the moment. If you think I should, I will add it.

Also, how do you know that the cookie is really set? I use the Firefox plugin "Web Developer" (currently 1.2.1 version), to help me figure things out, when a cookie is set, the domain name, and the information that is set.

I know it is set because I use the following code to display a phone# on the page based on the cookie data:

Code:
                <script type="text/javascript">
                if (getCookie("localnumber") != null)
                {
                document.write(getCookie("localnumber"));
                }
                else
                {
                document.write(800-851-2021);
                }
                </script>

Based on that, I'm fairly certain it is set, though I very well could be wrong.

If you would like, I can take a look at your code, program, and see where the mistake is occurring. PM if you are interested. The guides I gave should have solved all the problems, but it sounds like there is something outside the vacuum that may be occurring.

Thank you! PM headed your way...
 
If anyone is interested, the problem was solved by declaring the path as the root, and the main domain as a solution, example in the setcookie we changed the line of code to:

document.cookie="localnumber=" + cookievalue + "; path=/; domain=.domainname.com;";

Solved.

We probably should set an expiration date, etc... but then that's be all fancy and correct.

</thread>
 
Kudos to CCarter for solving the problem!

If anyone else runs into something similar, here was the solution:

Code:
<!--
function setCookie()
{
    cookievalue= "value" + ";";
    var expiryDate = new Date();
    var oneYearFromNow = expiryDate.getTime() + (365 * 24 * 60 * 60 * 1000);
    expiryDate.setTime(oneYearFromNow);
    document.cookie="localnumber=" + cookievalue + "; expires=" + expiryDate.toString() + "; path=/; domain=.yourdomain.com;";
}
setCookie();

    function getCookie(name)
{    
    var re = new RegExp(name + "=([^;]+)");
    var value = re.exec(document.cookie);
    return (value != null) ? unescape(value[1]) : null; 
}
getCookie();
//-->
 
Hey Rage9, if it's so frustrating for you to read this, then fuck off and don't read it you whiny little bitch.

The purpose of this forum is for people to ask questions. You didn't magically wake up one day and all of the sudden know everything you know. Now stick your mothers dildo back in your pie hole and shut the fuck up.

Fuck off douche bag. Maybe learn some JS instead of bumbling around like the fucking moron you are. Why do that though when you can cry like a bitch and get someone else to do it for ya? Get a clue.