You know how those pop up advertisements work? the ones that are super annoying when you hover over them. I want to create something similar.
What I want to do is given specific per-determined words with a given “definition” associated with those words, like a key value pair, search my web page for those words then make those words if clicked on pop up a little tooltip giving that words definition, the value.
Now I can kinda piece this together but I want to do it the most efficient way with caching the dictionary and caching as much as possible. so I don’t have to create two arrays every time the page loads then check if the words on the page match anything in the dictionary and if so make a tooltip or little pop up.
Can anyone give me any ideas or even code hints/links to help me get the best results/load times so It doesn’t take forever every time the web page loads.
I haven’t started making any of it yet, so I can go any route, I just want it to be fast.
Take your text container element and
split(' ')it’s content by words. Iterate over this array of words and wrap those of the words that are in the dictionary with aspanor some other element. Then,join(' ')the array back and replace the container’sinnerHTMLwith it.Next, add a listener to the container with a handler that will react only if
e.targetis aspan.The function
lookUpWordwill analyze thespan‘s text and draw the looked up definition.As for the format of the dictionary, it’d better be a simple JSON object, like this one:
A lookup in such an object would be as trivial as:
To check if a word is in the dictionary: