Computer Science Canada

Some Useful Javascripts

Author:  [Gandalf] [ Mon Jul 11, 2005 9:23 pm ]
Post subject:  Some Useful Javascripts

Well, I was recently working with JS for a while, and I accumulated a few pretty useful scripts.

This script shows some information about the users browser. You can easily change this code to customize the page according to the different capabilities available. I didn't create this one, just modified it.
code:
<center><font size = 4>Browser Information:</font size></center>
<script language="JavaScript">
var xy = navigator.appVersion;
xz = xy.substring(0,4);
document.write("<center><table border=1 cellpadding=2><tr><td>");
document.write("<center><b>", navigator.appName,"</b>");
document.write("</td></tr><tr><td>");
document.write("<center><table border=1 cellpadding=2><tr>");
document.write("<td>Browser Name: </td><td><center>");
document.write("<b>", navigator.appCodeName,"</td></tr>");
document.write("<tr><td>Version: </td><td><center>");
document.write("<b>",xz,"</td></tr>");
document.write("<tr><td>Platform: </td><td><center>");
document.write("<b>", navigator.platform,"</td></tr>");
document.write("<tr><td>Pages Viewed: </td><td><center>");
document.write("<b>", history.length," </td></tr>");
document.write("<tr><td>Java Enabled: </td><td><center><b>");
if (navigator.javaEnabled()) document.write("Yes</td></tr>");
else document.write("No</td></tr>")
document.write("<tr><td>Screen Resolution: </td><td><center>");
document.write("<b>",screen.width," x ",screen.height,"</td></tr>");
document.write("</table></tr></td></table></center>");
</script>


This script makes sure that both IE users and netscape (mozilla or firefox or many others) ones can't right click on your site. This includes no viewing of your source code (although there are various ways to get around this).
code:
<script language=JavaScript>
function noclickIE()
{
        if (document.all)
        {
                return false;
        }
}
function noclickNS(x)
{
        if (document.layers||(document.getElementById&&!document.all))
        {
                if (x.which==2||x.which==3)
                {
                        return false;
                }
        }
}
if (document.layers)
{
        document.captureEvents(Event.MOUSEDOWN);
        document.onmousedown=noclickNS;
}
else
{
        document.onmouseup=noclickNS;
        document.oncontextmenu=noclickIE;
}
document.oncontextmenu=new Function("return false")
</script> 


This function is useful if you want to scroll down the site automatically. I found this useful for automatically scrolling down the page when there are ads so that half of your openign page isn't covered. Sadly, this doesn't always work. If you want the page to automatically scroll when it loads you have to add this to your body tag: <body onLoad="jumpScroll()">.
code:
<script language=JavaScript>
function jumpScroll()
{
        window.scroll(0,30); // horizontal/vertical
}
</script>


This simple code puts the previous page you visited, only if there was a page though.
code:
<script language="JavaScript">
document.write("<b>You came from: " + document.referrer + "</b>");
</script>


Hope some of them come in handy. This is pretty much how I learned some of JavaScript, just looking at examples, maybe you will too! I will keep adding more as I learn and explore Smile.


: