简体   繁体   English

如何修复未定义的JavaScript错误?

[英]How to fix a not defined javascript error?

I am fairly new to Javascript and what I am trying to do is have a cookie set as soon as I click on a link. 我对Java语言还很陌生,我想做的就是在单击链接后立即设置一个cookie。 When I return back to the previous page from the link, I want the page to auto refresh and notify the user by color to show what link they just clicked. 当我从链接返回到上一页时,我希望页面自动刷新并通过颜色通知用户以显示他们刚刚单击的链接。 I used this example to guide me http://webdesign.about.com/od/cookies/a/aa083198.htm . 我使用此示例来指导我http://webdesign.about.com/od/cookies/a/aa083198.htm But I am still not getting it. 但是我还是不明白。

The code below is what I have. 下面的代码是我所拥有的。 The problem is that as soon as I click on the link firebug brings up the error "getLink not defined". 问题是,一旦我单击链接,萤火虫就会弹出错误“ getLink not defined”。 Also through web developer on Firefox, it seems that my cookie is not actually being set even though I am calling it from the Html.I am also showing gave the most important part in my Html that calls the function. 同样通过Firefox上的Web开发人员,即使我从Html调用它也似乎实际上没有设置我的cookie,并且还展示了Html中调用函数的最重要部分。

The videoId i have in setCookie is a php variable that is defined somewhere else in my code. 我在setCookie中拥有的videoId是一个在我的代码中其他位置定义的php变量。 I would really appreciate it if someone can point me in the right direction. 如果有人能指出我正确的方向,我将不胜感激。 Thanks! 谢谢!

   <head>
    <script language="text/javascript">
    var cookie_name = "watched";
        function setCookie(cookie_name,cookie_value)
        {

        if (document.cookie!="") {
           index = document.cookie.indexOf(cookie_name);
           } else {
            index = -1;
            }
            if (index == -1) {
              var finish = 7200;
              var cookie_value = videoId + "; expires=" + finish;
              document.cookie=cookie_name  + "=" + cookie_value;
              }         
            }

     function getLink(cookie_value) {     
       if (document.cookie) {
         index = document.cookie.indexOf(cookie_value);
             if (index != -1) {
             colorLinks;
              }
            else{
            //alert("No color");
             }

              }
            return colorLinks;
           }

     function colorLinks()
      {
        $('#' + videoId).css('background-color: pink'); 

      }
  </script>
  </head>
<body onLoad=window.refresh>
<div id="page">
echo '<a href="' . $link . '" onclick="setCookie(); return true;">' . $this->Links

This does not make sense: 这根本不符合逻辑:

function getLink(cookie_value) {  
  if (document.cookie) {
    index = document.cookie.indexOf(cookie_value);
    if (index != -1) {
      colorLinks; // you mean colorLinks(); ???
    }
    else {
      //alert("No color");
    }
  }
 // why return a function that sets color instead of just calling it?
  return colorLinks;
}

and there is luckily nothing called window.refresh (unless you have created it yourself) or you would loop forever 幸运的是,没有什么叫做window.refresh (除非您自己创建了它),否则您将永远循环

  1. In the code i don't see where videoId is given a value 在代码中,我看不到videoId的值
  2. You don't call the function setCookie(cookie_name,cookie_value); 您不调用函数setCookie(cookie_name,cookie_value); to set the cookie. 设置cookie。

Please read about cookies: http://www.quirksmode.org/js/cookies.html 请阅读有关cookie的信息: http : //www.quirksmode.org/js/cookies.html

<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;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM