Javascript 无法识别函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5216631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:24:06  来源:igfitidea点击:

Javascript does not recognize function

javascriptjqueryfirefoxdom

提问by VJune

I am new to programming in Javascript and this one has clearly 'clean bowled' me :(........

我是 Javascript 编程的新手,这个人显然已经“干净利落地”了我 :(........

The following code snippet highlights some text by searching for and modifying the respective text nodes. Text node searching is done by using a Jquery functionality:

以下代码片段通过搜索和修改相应的文本节点来突出显示一些文本。文本节点搜索是通过使用 Jquery 功能完成的:

window.addEventListener("load", highlightSummarySentences , false);

window.addEventListener("load", highlightSummarySentences , false);

function highlightSummarySentences() {
    var docName = thisPage;
    var numSentences = getCookie(docName+"Num");
    var linkSentenceNum = getCookie(docName + 'LinkingSentence');
    for(var i=0; i<numSentences; i++) {
        var matchMe = getCookie(docName+i);
        try {
            if (matchMe && i==(linkSentenceNum)) {
                highlightText(matchMe, clickedSentenceColour);
            }
            else if(matchMe){
                highlightText(matchMe, summarySentenceColour);
            }
        } catch (e) {;
        }
    }
}

The for loop runs once and highlights the appropriate text after which it exits and the page becomes completely white. The following error is seen in the error console:

for 循环运行一次并突出显示适当的文本,然后退出并且页面变为完全白色。在错误控制台中看到以下错误:

Error: getCookie is not defined

错误:未定义 getCookie

but I guess its not getCookie. The script just refuses to recognize any function or variable after the above event. I have no idea what might be causing the script to behave this way. I am developing in firefox.

但我想它不是 getCookie。在上述事件之后,脚本只是拒绝识别任何函数或变量。我不知道是什么导致脚本以这种方式运行。我正在Firefox中开发。

Please give me a hint! Let me know if I should paste more code of give more information.

请给我一个提示!让我知道是否应该粘贴更多代码以提供更多信息。

Thanks,

谢谢,

采纳答案by Dr.Molle

Your description(page clears after 1st loop) sounds like you use somewhere document.write().

您的描述(第 1 次循环后页面清除)听起来像是您在某处使用document.write()

Do you? (could be in highlightText())

你?(可以在 highlightText() 中)

If yes: you can't use write() after a document is loaded, write() will overwrite all contents, js too, so all functions defined somewhere doesn't exist anymore.

如果是:在加载文档后不能使用 write(),write() 将覆盖所有内容,js 也是如此,因此在某处定义的所有函数不再存在。

回答by Alex Wayne

getCookie()is not a function provided by the browser. This snippet probably expects a getCookie()function to be declared somewhere in your code. document.cookieis what you would use to get the cookie string from a browser, but you have to parse and split it to find the data you need. This is what a getCookiefunction you write would do for you.

getCookie()不是浏览器提供的功能。此代码段可能需要getCookie()在代码中的某处声明一个函数。 document.cookie是您用来从浏览器获取 cookie 字符串的方法,但您必须对其进行解析和拆分才能找到所需的数据。这就是getCookie您编写的函数将为您做的事情。

回答by disappearedng

If you are tagging jquery, I recommend you to do the following:

如果您要标记 jquery,我建议您执行以下操作:

<script>
$(document).ready(function() {
   window.addEventListener("load", highlightSummarySentences , false); });
</script>

回答by murali krish

Add this getCookie() function in your page

在您的页面中添加此 getCookie() 函数

function getCookie(cname) {
     var name = cname + "=";
     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);
        if(c.indexOf(name) == 0)
           return c.substring(name.length,c.length);
     }
     return "";
}