More Javascript Tips
- Disable Right Click On Images Only
- Check if a Variable Exists
- Passing Arguments to The Onload Event Function
Disable Right Click On Images Only
function disable_right_click(e)
{
var browser = navigator.appName.substring ( 0, 9 );
var event_number = 0;
if (browser=="Microsoft")
event_number = event.button;
else if (browser=="Netscape")
event_number = e.which;
if ( event_number==2 || event_number==3 )
{
alert ("Right Mouse Button Is Disabled");
return (false);
}
return (true);
}
function trap_images_mouse_events ()
{
if ( document.images )
{
for (var pic=0; pic<document.images.length; pic++)
document.images[pic].onmousedown = disable_right_click;
}
}
window.onload = trap_images_mouse_events;
Check if a Variable Exists
This problem often occurs if code tries to use a variable that has not been defined before. Then you get 'a variable is not defined' error.One of the solutions
is to use window.variableName.The usage is simple: if(window.variableName)...
Passing Arguments to The Onload Event Function
The onload event handler can be used to attach a function that will be executed when the page has finished loading. A reference to the function is passed as follows:
There is a problem with this approach that no arguments can be passed to the function using this syntax. The solution is to use an anonymous function: