5 Cool and Useful jQuery Tricks

1. Disable Right Click

The code snippet below disables right click on an entire page, but you can make it work on only particular page elements by changing the selector from document to something more specific.

[javascript]
$(document).ready(function(){
$(document).bind("contextmenu", function(e){
return false;
})
})
[/javascript]

2. Set a Timer
The following code snippet sets off an alert exactly 3 seconds after the page has loaded.

[javascript]
$(document).ready(function(){
setTimeout(function(){
alert("Surprise!");
}, 3000);
})
[/javascript]

3. Browser Detection
jQuery can easily detect which browser a page is being viewed on, and can even be used to apply style or make changes to HTML elements depending on which browser is being used. The code below changes the padding on all p elements when the browser is Safari.

[javascript]
if($.browser.safari) $("p").css("padding", "1em");
[/javascript]

4. Dynamically Alter Links
This one is particularly useful if you want to add target="_blank" to all the links on a particular page without having to go through and do it manually.

[javascript]
$('a[@rel$='external']').click(function(){
this.target = "_blank";
});
[/javascript]

5. Check if Elements Exist
Avoid errors by writing code like this one below that checks if an element exists before it executes the action.

[javascript]
if ($("#someElement").length)
{
//The DOM element exists
}
[/javascript]



Responsive Menu
Add more content here...