Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7217845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:24:18+00:00 2026-05-28T21:24:18+00:00

I’m studying Javascript cookie. Basically what I want is when the first time user

  • 0

I’m studying Javascript cookie. Basically what I want is

  1. when the first time user input their number it will save the cookie name, value and expiration date to 365.

  2. When the user visit again my site he/she don’t have to enter his number anymore as long as the cookie is still alive or haven’t deleted yet in the browser, he/she will be redirected to my homepage.

Here’s my code in javascript so far:

function checkCookie() {
    var mob_tel=getCookie("mob_tel");
    if (mob_tel!=null && mob_tel!="") {
        alert("Welcome again " + mob_tel);
    } else {
        set_name("");
    }
}

function set_name(form) {
    var mob_tel = form.mobtel.value
    if (mob_tel != "") {
        if (confirm("Are you sure you want this saved as your number?")) {
            setCookie ("mob_tel", mob_tel, 365);
            //window.history.go(0);
        }
    } else alert("Geez, at least enter something, entering nothing will cause an error.");
}

my html body

<body onload="checkCookie()">
    <form>
            Enter Mobile Number: <input type="text" name="mobtel"><br>
            <input type="submit" value="Save Cookie" onclick="set_name(this.form)">
    </form>
</body>

It all works but in my firebug:

Uncaught TypeError: Cannot read property 'value' of undefined
set_name
checkCookie
(anonymous function)
onload

Maybe my coding style is wrong. I’m open to rewrite my code. I’m still new to javascript.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T21:24:19+00:00Added an answer on May 28, 2026 at 9:24 pm

    Here’s a working version.

    This is what I did:
    1 – getCookie() and setCookie() weren’t defined. I used the W3Schools functions to supplement them.
    2 – There were some scripting errors (missing semi-colons,etc) I fixed those.
    3 – for the set_name() function, I changed it to a DOM query to access the input which held the telephone number.

    <script>
    function checkCookie() {
        var mob_tel=getCookie("mob_tel");
        if (mob_tel!=null && mob_tel!="") {
            alert("Welcome again " + mob_tel);
        } else {
            set_name("");
        }
    }
    
    function getCookie(c_name) {
        var i,x,y,ARRcookies=document.cookie.split(";");
        for (i=0;i<ARRcookies.length;i++) {
            x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
            x=x.replace(/^\s+|\s+$/g,"");
            if (x==c_name) {
                return unescape(y);
            }
        }
    }
    
    function setCookie(c_name,value,exdays) {
        var exdate=new Date();
        exdate.setDate(exdate.getDate() + exdays);
        var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
        document.cookie=c_name + "=" + c_value;
    }
    
    function set_name(form) {
        var mob_tel = document.getElementById('mobtel').value;
        if (mob_tel != "") {
            if (confirm("Are you sure you want this saved as your number?")) {
                setCookie ("mob_tel", mob_tel, 365);
                //window.history.go(0);
            }
        } else alert("Geez, at least enter something, entering nothing will cause an error.");
    }
    </script>
    <body onload="checkCookie()">
        <form>
                Enter Mobile Number: <input type="text" name="mobtel" id="mobtel"><br>
                <input type="submit" value="Save Cookie" onclick="set_name(this.form)">
        </form>
    </body>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to clean up various Word 'smart' characters in user input, including but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.