I've noticed that some problems come up frequently that are of importance in
writing web pages, because they're pretty fundamental points.
For general reference, here are some collected solutions.
1. Opens a new Window at maximum size:
window.moveTo(0 ,0,screenX=0,sc reenY=0)
window.resizeTo (screen.availWi dth+2,screen.av ailHeight+6)
2. Defeats Frames (including Hotmail frames):
if (window != top)
top.location.hr ef = location.href
3. Fixes a CSS Bug in Netscape Navigator:
function WM_netscapeCssF ix() {
if (document.WM.WM _netscapeCssFix .initWindowWidt h !=
window.innerWid th || document.WM.WM_ netscapeCssFix. initWindowHeigh t !=
window.innerHei ght) {
document.locati on = document.locati on;
}
} function WM_netscapeCssF ixCheckIn() {
if ((navigator.app Name == 'Netscape') && (parseInt(navig ator.appVersion )
== 4)) {
if (typeof document.WM == 'undefined'){
document.WM = new Object;
}
if (typeof document.WM.WM_ scaleFont == 'undefined') {
document.WM.WM_ netscapeCssFix = new Object;
document.WM.WM_ netscapeCssFix. initWindowwidth =window.innerWi dth;
document.WM.WM_ netscapeCssFix. initWindowheigh t=window.innerH eight;
}
window.onresize = WM_netscapeCssF ix;
}
} WM_netscapeCssF ixCheckIn()
4. Preloads images:
if (document.image s)
{
pic1= new Image(800,10);
pic1.src="image s/image1.gif";
pic2= new Image(15,5);
pic2.src="image s/image2.gif";
pic3= new Image(50,450);
pic3.src="image s/image3.gif";
}
5. Opens a new Window with all window "furniture" :
function openNewWin(url)
{
var atts =
'menubar=yes,to olbar=yes,locat ion=yes,status= yes,scrollbars= yes,resizable=y e
s,directories=y es,menubar=1,to olbar=1,locatio n=1,status=1,sc rollbars=1,resi z
able=1,director ies=1';
var newWindow = open (url,'_blank',a tts);
newWindow.focus ()
}
- and is called in a HTML document (as shown in the examples below) by using
the syntax openNewWin(inde x.html) or openNewWin(this .href)
6. Opens a new Window in maximum size (no window "furniture" ):
function openMaxWin(url)
{
var atts =
'menubar=no,too lbar=no,locatio n=no,status=yes ,scrollbars=yes ,resizable=yes, m
enubar=0,toolba r=0,location=0, status=1,scroll bars=1,resizabl e=1';
var newWindow = open (url,'_blank',a tts);
newWindow.focus ()
}
7. Opens a small pop-up type Window:
function openPopWin(url)
{
var atts =
'top=0,left=0,m enubar=no,toolb ar=no,location= no,status=no,sc rollbars=no,res i
zable=no,screen X=0,screenY=0,m enubar=0,toolba r=0,location=0, status=0,scroll b
ars=0,resizable =0,width=200,he ight=250';
var newWindow = open (url,'_blank',a tts);
newWindow.focus ()
}
8. Opens a new Window in fullscreen:
function fullscreen(url)
{
w = screen.availWid th-10;
h = screen.availHei ght-20;
features = "width="+w+",he ight="+h;
features += ",left=0,top=0, screenX=0,scree nY=0";
window.open(url , "", features);
}
9. A link to another page of your site, in a form that will work even with a
browser which does not have Javascript enabled:
<a href="filename. htm" target="_blank"
onClick="window .open(this.href ,'','menubar=no ,toolbar=no,loc ation=no,status =
yes,scrollbars= yes,resizable=y es');return false;">Click here</a>
10. Returns the date the page was last updated:
function lastupdated()
{
lastmod = new Date(document.l astModified);
var year = lastmod.getYear ();
if (year < 2000)
{
year = year + 1900;
}
var day = new Array;
day[0] = "Sunday";
day[1] = "Monday";
day[2] = "Tuesday";
day[3] = "Wednesday" ;
day[4] = "Thursday";
day[5] = "Friday";
day[6] = "Saturday";
var month = new Array;
month[1] = "January";
month[2] = "February";
month[3] = "March";
month[4] = "April";
month[5] = "May";
month[6] = "June";
month[7] = "July";
month[8] = "August";
month[9] = "September" ;
month[10] = "October";
month[11] = "November";
month[12] = "December";
function suffix()
{
var d = new Date(document.l astModified).ge tDate();
var s = "th";
if ((d == 1) || (d == 21) || (d == 31))
{
s = "st";
}
else if ((d == 2) || (d == 22))
{
s = "nd";
}
else if ((d == 3) || (d == 23))
{
s = "rd";
}
return s;
}
var updtime = month[lastmod.getMont h()+1] + " " + lastmod.getDate () + ", "
+ year;
return updtime;
}
document.write( "<font class='updateti me'>" + lastupdated() + "</font>");
11. Ways to call a function in a HTML page:
(a) Using onLoad event handler:
<body onLoad="functio nName()">
(b) Using onClick event handler:
<a href="filename. htm" onClick="functi onName();return false;">Click here</a>
Or to link to an <a name="abc"></a> style link on the same page:
<a href="filename. htm#abc" onClick="window .location.reloa d()">Click here for
#abc link</a><br>
Or to open a new Window in which all the usual Window "furniture" is wanted:
<a href="index.htm " title="Homepage " onClick="openNe wWin(this.href) ;return
false"><u>Home</u></a>
- which calls the function openNewWin() defined above, and does so by using
the special expression "this.href" which can be used to refer back to the
definition specified for href, ie in this case index.htm.
(c) To automatically load a set of standard scripts from an external .js
file located in the subfolder "/scripts", put this in the page HEAD, ie
before </head>:
<script language=JavaSc ript src="scripts/filename.js"
type=text/javascript></script>
(d) To print the result from a javascript function in the BODY of a page
(formatted by a CSS style "class" function), add the following in the <BODY>
of the HTML document:
document.write( "<font class='classnam e'>" + functionName() + "</font>");
writing web pages, because they're pretty fundamental points.
For general reference, here are some collected solutions.
1. Opens a new Window at maximum size:
window.moveTo(0 ,0,screenX=0,sc reenY=0)
window.resizeTo (screen.availWi dth+2,screen.av ailHeight+6)
2. Defeats Frames (including Hotmail frames):
if (window != top)
top.location.hr ef = location.href
3. Fixes a CSS Bug in Netscape Navigator:
function WM_netscapeCssF ix() {
if (document.WM.WM _netscapeCssFix .initWindowWidt h !=
window.innerWid th || document.WM.WM_ netscapeCssFix. initWindowHeigh t !=
window.innerHei ght) {
document.locati on = document.locati on;
}
} function WM_netscapeCssF ixCheckIn() {
if ((navigator.app Name == 'Netscape') && (parseInt(navig ator.appVersion )
== 4)) {
if (typeof document.WM == 'undefined'){
document.WM = new Object;
}
if (typeof document.WM.WM_ scaleFont == 'undefined') {
document.WM.WM_ netscapeCssFix = new Object;
document.WM.WM_ netscapeCssFix. initWindowwidth =window.innerWi dth;
document.WM.WM_ netscapeCssFix. initWindowheigh t=window.innerH eight;
}
window.onresize = WM_netscapeCssF ix;
}
} WM_netscapeCssF ixCheckIn()
4. Preloads images:
if (document.image s)
{
pic1= new Image(800,10);
pic1.src="image s/image1.gif";
pic2= new Image(15,5);
pic2.src="image s/image2.gif";
pic3= new Image(50,450);
pic3.src="image s/image3.gif";
}
5. Opens a new Window with all window "furniture" :
function openNewWin(url)
{
var atts =
'menubar=yes,to olbar=yes,locat ion=yes,status= yes,scrollbars= yes,resizable=y e
s,directories=y es,menubar=1,to olbar=1,locatio n=1,status=1,sc rollbars=1,resi z
able=1,director ies=1';
var newWindow = open (url,'_blank',a tts);
newWindow.focus ()
}
- and is called in a HTML document (as shown in the examples below) by using
the syntax openNewWin(inde x.html) or openNewWin(this .href)
6. Opens a new Window in maximum size (no window "furniture" ):
function openMaxWin(url)
{
var atts =
'menubar=no,too lbar=no,locatio n=no,status=yes ,scrollbars=yes ,resizable=yes, m
enubar=0,toolba r=0,location=0, status=1,scroll bars=1,resizabl e=1';
var newWindow = open (url,'_blank',a tts);
newWindow.focus ()
}
7. Opens a small pop-up type Window:
function openPopWin(url)
{
var atts =
'top=0,left=0,m enubar=no,toolb ar=no,location= no,status=no,sc rollbars=no,res i
zable=no,screen X=0,screenY=0,m enubar=0,toolba r=0,location=0, status=0,scroll b
ars=0,resizable =0,width=200,he ight=250';
var newWindow = open (url,'_blank',a tts);
newWindow.focus ()
}
8. Opens a new Window in fullscreen:
function fullscreen(url)
{
w = screen.availWid th-10;
h = screen.availHei ght-20;
features = "width="+w+",he ight="+h;
features += ",left=0,top=0, screenX=0,scree nY=0";
window.open(url , "", features);
}
9. A link to another page of your site, in a form that will work even with a
browser which does not have Javascript enabled:
<a href="filename. htm" target="_blank"
onClick="window .open(this.href ,'','menubar=no ,toolbar=no,loc ation=no,status =
yes,scrollbars= yes,resizable=y es');return false;">Click here</a>
10. Returns the date the page was last updated:
function lastupdated()
{
lastmod = new Date(document.l astModified);
var year = lastmod.getYear ();
if (year < 2000)
{
year = year + 1900;
}
var day = new Array;
day[0] = "Sunday";
day[1] = "Monday";
day[2] = "Tuesday";
day[3] = "Wednesday" ;
day[4] = "Thursday";
day[5] = "Friday";
day[6] = "Saturday";
var month = new Array;
month[1] = "January";
month[2] = "February";
month[3] = "March";
month[4] = "April";
month[5] = "May";
month[6] = "June";
month[7] = "July";
month[8] = "August";
month[9] = "September" ;
month[10] = "October";
month[11] = "November";
month[12] = "December";
function suffix()
{
var d = new Date(document.l astModified).ge tDate();
var s = "th";
if ((d == 1) || (d == 21) || (d == 31))
{
s = "st";
}
else if ((d == 2) || (d == 22))
{
s = "nd";
}
else if ((d == 3) || (d == 23))
{
s = "rd";
}
return s;
}
var updtime = month[lastmod.getMont h()+1] + " " + lastmod.getDate () + ", "
+ year;
return updtime;
}
document.write( "<font class='updateti me'>" + lastupdated() + "</font>");
11. Ways to call a function in a HTML page:
(a) Using onLoad event handler:
<body onLoad="functio nName()">
(b) Using onClick event handler:
<a href="filename. htm" onClick="functi onName();return false;">Click here</a>
Or to link to an <a name="abc"></a> style link on the same page:
<a href="filename. htm#abc" onClick="window .location.reloa d()">Click here for
#abc link</a><br>
Or to open a new Window in which all the usual Window "furniture" is wanted:
<a href="index.htm " title="Homepage " onClick="openNe wWin(this.href) ;return
false"><u>Home</u></a>
- which calls the function openNewWin() defined above, and does so by using
the special expression "this.href" which can be used to refer back to the
definition specified for href, ie in this case index.htm.
(c) To automatically load a set of standard scripts from an external .js
file located in the subfolder "/scripts", put this in the page HEAD, ie
before </head>:
<script language=JavaSc ript src="scripts/filename.js"
type=text/javascript></script>
(d) To print the result from a javascript function in the BODY of a page
(formatted by a CSS style "class" function), add the following in the <BODY>
of the HTML document:
document.write( "<font class='classnam e'>" + functionName() + "</font>");
Comment