Wednesday, October 21, 2009

Code for "Bookmark Us" in Javascript

Place the following script into your <head>section in the page you need to use it:

<script type="text/javascript">

function bookmark_us(url, title){

if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}
</script>

Next, place the following code in order to show the link that opens browser's bookmark window:

<a href="javascript:bookmark_us('http://phpcoders.blogspot.com/','Php Coders')">Bookmark us!</a>

That is all! You have now a full working bookmark us script.

JavaScript Wait While Loading Page Display Image

If you have a page that takes long time to display it is a good idea to display a "wait until the page loads" image. This tutorial show you how to implement this in your webpage.

Below are the steps to implement the feature:

Step 1. Every time your page loads a "init()" function will load.

<body onLoad="init()">

Step 2. Define a div named "loading" right after <body> section.

<div id="loading" style="position:absolute; width:100%; text-align:center; top:300px;">
<img src="loading.gif" border=0></div>

The loading.gif image should be an animated gif that suggests that the page is still loading.

Step 3. Place this javascript code right after you define the div.

<script>
var ld=(document.all);
var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
if (ns4)
ld=document.loading;
else if (ns6)
ld=document.getElementById("loading").style;
else if (ie4)
ld=document.all.loading.style;
function init()
{
if(ns4){ld.visibility="hidden";}
else if (ns6||ie4) ld.display="none";
}
</script>

How to diable Right Click in JavaScript?

Neat code to restrict a user to right click and view the code or save an image. Though, it does not assure complete safety for your source (there is always a way to copy them), but it is better than nothing.

Copy & Paste this script under the HEAD in the HTML area of your webpage.

<script>
var isNS = (navigator.appName == "Netscape") ? 1 : 0;
if(navigator.appName == "Netscape") document.captureEvents(Event.MOUSEDOWN||Event.MOUSEUP);
function mischandler(){
return false;
}
function mousehandler(e){
var myevent = (isNS) ? e : event;
var eventbutton = (isNS) ? myevent.which : myevent.button;
if((eventbutton==2)||(eventbutton==3)) return false;
}
document.oncontextmenu = mischandler;
document.onmousedown = mousehandler;
document.onmouseup = mousehandler;
</script>