Sometimes some little scripts can bring about magic to your site. This tutorial includes a collection of useful scripts that surely will spice up your pages. This article covers page redirection, displaying today’s date, closing window, setting page as ‘home page’ and adding page to ‘favorites’.
Page Redirection
Using JavaScript, it is possible to redirect viewers to a page instantly. Here’s the code, which must go in your <HEAD> section:
Today’s Date
Visitors are always confused and putting today’s date on your page will make life easier for them. Here’s the code:
|
<SCRIPT LANGUAGE = "JavaScript"> <!--
// Array of day names var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthNames = new Array("January","February","March","April","May","June","July", "August","September","October","November","December");
var dt = new Date(); var y = dt.getYear();
// Y2K compliant if (y < 1000) y +=1900;
document.write(dayNames[dt.getDay()] + ", " + monthNames[dt.getMonth()] + " " + dt.getDate() + ", " + y); // --> </SCRIPT> |
Closing Window
Very often we use pop-ups in our site and sometimes we disable the close button option. Providing the users with a close window option with JavaScript can be as simple as that:
|
<form> <input type=button value="Close Window" onClick="javascript:window.close();"> </form> or <a href="javascript:window.close();">Close Window</a> |
Note that I have included the ‘close window’ function in 2 forms; a button and a link.
Setting your page as Home Page
Providing a ‘Make Home Page’ option to your site might encourage people to do that. It is possible to set this script to an onLoad event handler so that it happens as soon as the user logs into your page. A little box pops up once the script runs, asking if the user really wants your page set to be the home page. Don't make them click "no" every time they come in. Keep everyone happy and let them choose whether to set your page as their home page or not. Here's the code:
<SPAN onClick="this.style.behavior='url(#default#homepage)'; this.setHomePage('http://www.edevcafe.com');"> Click Here to Make eDevCafe.com Your Home Page </SPAN> |
Note: You can change the cursor type, and text decoration colour and style.
Adding Page to Favorites
Ease your visitor’s journey by providing them a way to bookmark your URL. Here’s the code:
<U> <SPAN STYLE='color:blue;cursor:hand;' onClick='window.external.AddFavorite(location.href, document.title);'> Add this page to your favorites </U> |
Note:
- window.external states that an external window will be called upon.
- AddFavorite does just what it says. It adds something to your favorites menu.
- location.href, document.title is what it adds, the page's URL and the document.title.