Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript does not recognize function

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

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:

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:

Error: getCookie is not defined

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.

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

Thanks,

like image 219
VJune Avatar asked Mar 07 '11 06:03

VJune


1 Answers

getCookie() is not a function provided by the browser. This snippet probably expects a getCookie() function to be declared somewhere in your code. document.cookie is 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 getCookie function you write would do for you.

like image 191
Alex Wayne Avatar answered Oct 14 '22 07:10

Alex Wayne