Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check with JQuery, if a certain word is typed in the Browser?

This is a funny one, I want to display a certain div on a website only if a secret word is typed on that page (no forms are present). What is the easiest way to accomplish this with JQuery? Are there any Plugins? Thanx in advance cheers Tabaluga

like image 994
tabaluga Avatar asked Jul 11 '11 19:07

tabaluga


People also ask

How do you check if a string contains a word in jQuery?

jQuery :contains() Selector The :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).

How do I search for a word in jQuery?

We can use the replace() method with either regular expression or string as an argument. How to find the number of occurrences of a particular word or a string in a given string or paragraph. In this case, we will use the indexOf() Method in JavaScript to find the number of occurrences of desired word or substring.

How do I check if a div contains a text?

To check if a div element contains specific text:Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the div . If it is, the includes() method returns true , otherwise false is returned.

Does browser understand jQuery?

Browsers 'understand' JavaScript because it is built in. JavaScript understands jQuery because it is, after all, JavaScript under the hood, not a separate, discrete language. It is JS.


2 Answers

if ( window.addEventListener ) {
        var kkeys = [], konami = "68,73,78,78,69,82"; //this spells dinner
        window.addEventListener("keydown", function(e){
                kkeys.push( e.keyCode );
                if ( kkeys.toString().indexOf( konami ) >= 0 ) {
                    // run code here    
                    $("#text").hide().fadeIn("slow").html('Now the website will appear.');
                }
        }, true);
}

you can check what letters are what by doing:

if ( window.addEventListener ) {
    window.addEventListener("keydown", function(e){
        $("#text").append(e.keyCode + " ");
    }, true);
}
like image 57
switz Avatar answered Nov 15 '22 03:11

switz


try using js-Hotkey jquery plugin:

$(document).bind('keydown', 's+e+c+r+e+t', fn);

Also you may want to inspect through KonamiCodeWebsites to see how it works:

In this website you need to enter konami code (UP + UP + DN + DN + LFT + LFT + RGT + RGT + B + A) in order to enter the website!

like image 44
Mo Valipour Avatar answered Nov 15 '22 04:11

Mo Valipour