Javascript does not recognize function

I am new to Javascript programming, and this person has clearly β€œcleared me up”: (........

The following code fragment selects some text by searching and modifying the corresponding text nodes. Node text search is performed using 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 starts once and selects the corresponding text, after which it exits, and the page turns completely white. The following error is displayed in the error console:

Error: getCookie not defined

but I think this is not getCookie. The script simply refuses to recognize any function or variable after the above event. I have no idea what might make a script behave this way. I am developing in firefox.

Please give me a hint! Let me know if I should insert more code for more information.

Thanks,

+4
source share
4 answers

Your description (the page is cleared after the 1st cycle) sounds like you are using document.write() somewhere.

You? (maybe in highlightText ())

If yes: you cannot use write () after loading the document, write () will overwrite all contents, js too, so all functions defined somewhere else no longer exist.

+3
source

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

+5
source

If you tag jquery, I recommend you do the following:

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

Add this getCookie () function to your page

 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 ""; } 
0
source

Source: https://habr.com/ru/post/1342591/


All Articles