JavaScript,Cookies,body;看似未定义

时间:2013-03-27 01:19:11

标签: javascript cookies undefined

从我添加cookie开始,我之前的代码行不再有效。它告诉我以下是未定义的:greetUser()和touchrock()是未定义的。我似乎也遇到了yourOrder(),placeOrder()和resizePic()

的问题

基本上我的所有功能都在

<head>
    <title>Remembering Everything</title> 
    <link type="text/css" rel="stylesheet" href="first.css"> 

    <script type="text/javascript" src="cookie.js"></script> 

    <script type="text/javascript">

    function yourOrder() {
        const TAXRATE = 0.25;
        const DONUTPRICE = 0.50; 
        const TIME = 15; 
        var CakeDonuts = parseDonuts(document.getElementById("CakeDonuts").value);
        var GlazedDonuts = parseDonuts(document.getElementById("GlazedDonuts").value);
        if (isNaN(CakeDonuts))
            CakeDonuts = 0;
        if (isNaN(GlazedDonuts))
            GlazedDonuts = 0; 
        var subTotal = (CakeDonuts + GlazedDonuts) * DONUTPRICE; 
        var tax = subTotal * TAXRATE;
        var total = subTotal + tax; 
        document.getElementById("subTotal").value = "$" + subTotal.toFixed(2);
        document.getElementById("tax").value = "$" + tax.toFixed(2);
        document.getElementById("total").value = "$" + total.toFixed(2); 
        setTimeout("document.getElementById('adam').src='sandlerclickresized.jpg';",5000).style.height=(document.body.clientHeight-300)*0.2;
        }

    function parseDonuts (donutString) {
        numDonuts=parseInt(donutString);
        if (donutString.indexOf("dozen") !=-1)
            numDonuts *=12;
        return numDonuts;
        } 

    function placeOrder() {
        if (document.getElementById("name").value="")
            alert("I'm sorry but you must provide your name before submitting an order.");
        else if (document.getElementById("TIME").value="") 
            isNaN(document.getElementById("TIME").value)
            alert("I'm sorry but you must provide the number of minutes until pick-up before submitting an order.");

            // Submit the order to the server
            form.submit();
        }

    function resizePic() {
        document.getElementById("resizedPic").style.height=(document.body.clientHeight-300)*0.2;
        } 


    function touchrock() {
        if (userName) {
            alert("I am glad that you have returned, " + userName + "Let's continue searching for your dream car");
        }   
        else {  
            userName=prompt("Hi! Welcome to Car Land. What is your name?");
            if (userName) {
                alert("It is good to meet you," + userName + ".");
                writeCookie("sandler_username", userName, 5 * 365); 
            }
            }
            document.getElementById("lambo").src= "lamboandgirl.jpg";
            }

    function greetUser() {
        userName=readCookie("sandler_username");
        if (userName)
            alert("Hell0" + userName +", I missed you... Not; just kidding.");
        else
            alert('Hi! Welcome to Car Land. What is your name?'); 
        }       


    </script>



</head>

    <body onload="alert('Hello and welcome'); greetUser(); touchrock();" onresize="resizePic(); placeOrder(); parseDonuts(); touchrock(); yourOrder();"> 

        <center><h1 class="green">Today we are going to be doing something cool</h1></center>

        <center><img id="lambo" src="yes.jpg" alt="Lambourghini" title="Lambourghini" style="cursor:pointer" onclick="touchrock();" /></center>
        <center><img id="adam" src="adam.jpg" alt="adam" title="adam" /></center> 


            </br>
<p class="space">
            Name</br> <input type="text" id="name" name="name" value="" onchange="placeOrder();" /> </br> 
            # of cake donuts</br> <input type="text" id="CakeDonuts" name="CakeDonuts" value= "" onchange="yourOrder();" /> </br>
            # of glazed donuts</br> <input type="text" id="GlazedDonuts" name="GlazedDonuts" value= "" onchange="yourOrder();" /> </br>
            Minutes 'til pickup</br> <input type="text" id="TIME" name="TIME" value= "" onchange="placeOrder();" /> </br>
            Subtotal</br> <input type="text" id="subTotal" name="subTotal" value= "" onchange="yourOrder();" /> </br>
            Tax</br> <input type="text" id="tax" name="tax" value= "" onchange="yourOrder();" /> </br>
            Total</br> <input type="text" id="total" name="total" value= "" onchange="yourOrder();" /> </br>

        </br>

        <input type="button" value="Calculate Price" onclick="placeOrder();" /></br></br>

        <th>Choose Your Car:</th>
        <td>
        <select name="car">
        <option value="Lambo">Lambo</option>
        <option value="Ferrari">Ferrari</option>
        </td>

</P>

----------------------------------------------- -----下面是cookies.js ------------------

    function writeCookie(name, value, days) {
            //BY default, there s no expiraton so the cookie is temporary 
            var expires = "";

            //Specifying a numer of days makes the cookie persistant
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days * 24 * 60 * 60 *1000));
            expires= "; expires=" + date.toGMTString();
        }

            //set the cookie to the name, value, and expiration date
            document.cookie=name + "=" + value + expires + "; path=/";
        } 

    function readCookie(name) {
        //Find the specifies cookie and return its value
        var searchName=name + "=";
        var cookies=document.cookie.split(';');
        for (var i=0; i < cookies.length; i++) {
            var c=cookies[i];
            while (c.charAt(0) =='')
            c = c.substring(1, c.length);
            if (c.indexOf(searchName)==0)
            return c.substring(searchName.length, c.length);
        }

        return null;
        }

    function eraseCookie(name) {
        //Erase the specified cookie
        writeCookie (name, "", -1); 
        }

0 个答案:

没有答案