Some common things to implement in our webpage very frequently are as follows. How to implement all these I am going to tell you in this tutorial.
- Add To Favorite
- Set As Homepage
- Go To Top Of Page
- No Right Click
- Print Page
- Adding Current Date
- Adding Current Time
- Pop-Up Page Creation
- Closing Window
- Copyright Notice Updation
01. Add To Favorite
Someone may be interested in the content of your page. Offer him/her to add the page in his/her favorite menu. To do this you have to place the following code in your page.
<a href="javascript:window.external.AddFavorite('http://www.yoursite.com/', 'Site Title');">Add to favorite</a>
Put this code where you want to show this. This script only works for IE5+ browsers.
02. Set As Homepage
Someone may be interested to set your page as their homepage. Offer him/her to set the page as his/her homepage with the following code.
<SCRIPT LANGUAGE="JavaScript">
<A href="javascript:history.go(0)" onClick="javascript:this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.YourLink.com');" TARGET="_self">Set As HomePage</A>
</SCRIPT>
Put this code where you want to show this. This script only works for IE5+ browsers.
03. Go To Top Of Page
If your page is too long then always use this option. Offer him/her to go to the top of your page anytime.
<A HREF="javascript:scroll(0,0)">Go to top</A>
Put this code where you want to show this. This script works in all browsers.
04. No Right Click
Copy & paste the code below in between your <head> and </head>.
<script language=JavaScript>
<!--
var message="";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function("return false")
// -->
</script>
This silent script works in all browsers.
05. Automatic Page Print
Just copy & paste the code below where you want to show this.
<a href='javascript:;' onClick='window.print();return false'>Print this page.</a>
This script works in all browsers.
06. Adding Current Date In Webpage
To display the current date in your webpage just copy and paste the following code where you want to show it.
<script type="text/javascript">
var d = new Date()
document.write(d.getDate())
document.write(".")
document.write(d.getMonth() + 1)
document.write(".")
document.write(d.getFullYear())
</script>
This script works in all browsers.
07. Adding Current Time In Webpage
To display the current time in your webpage just copy and paste the following code where you want to show it.
<script type="text/javascript">
var d = new Date()
document.write(d.getHours())
document.write(":")
document.write(d.getMinutes())
document.write(":")
document.write(d.getSeconds())
</script>
This script works in all browsers.
08. Popup Window
Sometimes we see that we click on a link & it opens up in a new popup window of some size predefined say 300x200. At first copy the code below in your <head> & </head> of the page.
<SCRIPT LANGUAGE="JavaScript">
<!--
function Start(page)
{
OpenWin = this.open(page,"CtrlWindow","toolbar=No,menubar=No,location=No,
scrollbars=Yes,resizable=No,status=No,
width=550,height=470,left=150,top=150,"); }
//-->
</SCRIPT>
Now paste the code for your link where you want to display.
<a href="javascript:void(0);" class="navlink" onclick="javascript:Start ('link.html');">Your Link</a>
This script works in all browsers.
09. Closing Window
We might be interested to close a window with a link or a button. See the code.
Closing with button
<form>
<input type="button" value="Close Window" onClick="window.close()">
</form>
Closing with link
<a href="javascript: self.close()">Close Window</a>
This script works in all browsers.
10. Updating Coyright Notice
This copyright notice palces the current year on the page so that copyright notices are always current. Once it's on your page there is no need to update the script.
<script language = 'JavaScript'>
<!--
function y2k(number) { return (number < 1000) ? number + 1900 : number; }
var today = new Date();
var year = y2k(today.getYear());
document.write('© '+year+' all rights reserved');
//-->
</script>
This script works in all browsers.
Related Articles
- onMouseover? What's This?Well, its time to try out your first javascript. This one is nice because we dont have to deal with adding the script tag. This little script will write something of your choice to the browsers status bar when you move your mouse over a link. Lets look at the example:
- Javascripts For Your WebsiteHere is a list of short but invaluable Java scripts you can insert into your own site code to implement a number of small but extremely useful features.
- Using Link Tags with JavaScriptI get this question so much, I figured Id better get in gear and write another section to address using the link tag for javascripts (such as new windows), rather than using the old grey button. Well, there are a couple of ways to do this. Ill start with the easier to understand version first.
- Declaring Variables and FunctionsNow its time to get into some really fun stuff. Yes, variables and functions. Dont worry, its not as bad as it sounds.....lets start with declaring variables. Youll want to keep all of your variables in the HEAD section for now. Place the declarations between the SCRIPT tags inside the
- JavaScript Confirmation BoxesA javascript confirmation box can be a handy way to give your visitors a choice of whether or not an action is performed. A confirmation box will pop up much like an alert box, but will allow the viewer to press an OK or Cancel button.
- JavaScript AlertsWell, you want to add one of those JavaScript alert boxes that come out of nowhere, dont you? Okay, lets begin with the alert box that just tells the viewer something you want them to know. Heres the alert command:
- JavaScript Password ProtectionOK, I finally got around to writing the javascript password protection tutorial. You will see the example script here is pretty straightforward, but it is also pretty easy to get around. Ill tell you why and give you links to more secure scripts later in this tutorial.
- Opening a New WindowTo open a new window, you will need to use yet another ready-made JavaScript function. Here is what it looks like:
- Use This JavaScript To Hide Your EMail From SpambotsEver wander why so many webmasters refuse to insert their email address into the body of their webpages?
- Tools For Site DesignSite design: Layout Now that you know the four the elements of good site design, its time to put the knowledge to work. The objective of the website will dictate the content which in turn imposes conditions on navigation. Navigation, in turn, imposes conditions on the pages themselves. This is c....
