ad

Collection Of Important JavaScript Codes

js

Javascript code snippetJavaScript is the client side scripting language that can be used for making website interactive. In this post, I am adding few JavaScript codes that can be useful for you. These codes can work in all kind of website and content management systems (including blogger, WordPress).

Default Home Page – A JavaScript Code

If you want users to give an easy way to set your website as homepage, you can use the following javascript code.

<a href="#" onClick="this.style.behavior='url(#default#homepage)'; this.setHomePage('http://www.webtips.in');">Make Us Homepage</a>

Bookmark – A JavaScript Code

Although, all browsers support one click book mark to any website, you can give an additional link on your website to bookmark your website. Just use this simple code and allow users to bookmark your website just by clicking a link.

<SCRIPT LANGUAGE="JavaScript">
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {

var url="http://www.webtips.in";
var title="Website Development";

document.write('<A HREF="javascript:window.ext');
document.write('ernal.AddFavorite(url,title);" ');
document.write('onMouseOver=" window.status=');
document.write("'Add our site to your favorites!'; return true ");
document.write('"onMouseOut=" window.status=');
document.write("' '; return true ");
document.write('">Add our site to your favorites!</a>');
}
else {
var msg = "bookmark us!";
if(navigator.appName == "Netscape") msg += "  (CTRL-D)";
document.write(msg);
}
</script>

No-Right Click Javascript

Sometimes we want to disable right click function on our website to protecte images and content. For this, we can use this code.

<script language="JavaScript">

var message="that function is disabled.";

function click(e) {
if (document.all) {
if (event.button == 2) {
alert(message);
return false;
}
}
if (document.layers) {
if (e.which == 3) {
alert(message);
return false;
}
}
}
if (document.layers) {
document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;
</script>

Random Image Generator

If you want to display a random image each time a page loads, you can do it with JavaScript. Just use this simple code.

<script language="JavaScript">
var img_arr = new Array (“images1.gif", “images2.gif", “images3.gif", “images4.gif");
var i = Math.floor(4*Math.random());
document.write('< img src="' + img_arr[i] + '" width="100″ height="100″ alt="Random image" />');
</script>

Counting words entered in a text box

Sometimes we need to count words entered in the textarea. We also want this to be real-time counting and numbers should increase as typing in the text area would go on. See how it can be done

Form.  This is the code of the form. having a text area and a text field. Text field will show the count of chars

<form name="myform">
<textarea rows="15" name="text" cols="45"
onkeyup="count(this,document.myform.cnt)"></textarea>
<br />Word Count: <input type="text" name="cnt" value="0" size="5"
onkeyup="count(document.myform.text,this)" />
</form>

JavaScript function

function count(w,x){
var y=w.value;
var r = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z 0) r++;}
x.value=r;
}

Anti select script

This Javascript code is very useful for blog owners and web master to protect the content of their website from being copied and reproduced again.

<script language="JavaScript">

function disableselect(e){
return false
}

function reEnable(){
return true
}

//if IE4+
document.onselectstart=new Function ("return false")

//if NS6
if (window.sidebar){
document.onmousedown=disableselect
document.onclick=reEnable
}
</script>

These are simple code snippet that can be used in blogger and WordPress as well. Although you may use jQuery to implement these things with beautiful effects. But for simple working code, use these.

Tags: | |
Nord VPN Sale

Deepanker Verma is the founder of Techlomedia. He is a tech blogger, developer and gadget freak.


Similar Articles

0 Comments

Leave a comment

Comment policy: We love comments and appreciate the time that readers spend to share ideas and give feedback. However, all comments are manually moderated and those deemed to be spam or solely promotional will be deleted.

2020 UseThisTip | Developed By Deepanker