A
annon
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,screenY=0)
window.resizeTo(screen.availWidth+2,screen.availHeight+6)
2. Defeats Frames (including Hotmail frames):
if (window != top)
top.location.href = location.href
3. Fixes a CSS Bug in Netscape Navigator:
function WM_netscapeCssFix() {
if (document.WM.WM_netscapeCssFix.initWindowWidth !=
window.innerWidth || document.WM.WM_netscapeCssFix.initWindowHeight !=
window.innerHeight) {
document.location = document.location;
}
} function WM_netscapeCssFixCheckIn() {
if ((navigator.appName == 'Netscape') && (parseInt(navigator.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.innerWidth;
document.WM.WM_netscapeCssFix.initWindowheight=window.innerHeight;
}
window.onresize = WM_netscapeCssFix;
}
} WM_netscapeCssFixCheckIn()
4. Preloads images:
if (document.images)
{
pic1= new Image(800,10);
pic1.src="images/image1.gif";
pic2= new Image(15,5);
pic2.src="images/image2.gif";
pic3= new Image(50,450);
pic3.src="images/image3.gif";
}
5. Opens a new Window with all window "furniture":
function openNewWin(url)
{
var atts =
'menubar=yes,toolbar=yes,location=yes,status=yes,scrollbars=yes,resizable=ye
s,directories=yes,menubar=1,toolbar=1,location=1,status=1,scrollbars=1,resiz
able=1,directories=1';
var newWindow = open (url,'_blank',atts);
newWindow.focus ()
}
- and is called in a HTML document (as shown in the examples below) by using
the syntax openNewWin(index.html) or openNewWin(this.href)
6. Opens a new Window in maximum size (no window "furniture"):
function openMaxWin(url)
{
var atts =
'menubar=no,toolbar=no,location=no,status=yes,scrollbars=yes,resizable=yes,m
enubar=0,toolbar=0,location=0,status=1,scrollbars=1,resizable=1';
var newWindow = open (url,'_blank',atts);
newWindow.focus ()
}
7. Opens a small pop-up type Window:
function openPopWin(url)
{
var atts =
'top=0,left=0,menubar=no,toolbar=no,location=no,status=no,scrollbars=no,resi
zable=no,screenX=0,screenY=0,menubar=0,toolbar=0,location=0,status=0,scrollb
ars=0,resizable=0,width=200,height=250';
var newWindow = open (url,'_blank',atts);
newWindow.focus ()
}
8. Opens a new Window in fullscreen:
function fullscreen(url)
{
w = screen.availWidth-10;
h = screen.availHeight-20;
features = "width="+w+",height="+h;
features += ",left=0,top=0,screenX=0,screenY=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,location=no,status=
yes,scrollbars=yes,resizable=yes');return false;">Click here</a>
10. Returns the date the page was last updated:
function lastupdated()
{
lastmod = new Date(document.lastModified);
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.lastModified).getDate();
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.getMonth()+1] + " " + lastmod.getDate() + ", "
+ year;
return updtime;
}
document.write("<font class='updatetime'>" + lastupdated() + "</font>");
11. Ways to call a function in a HTML page:
(a) Using onLoad event handler:
<body onLoad="functionName()">
(b) Using onClick event handler:
<a href="filename.htm" onClick="functionName();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.reload()">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="openNewWin(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=JavaScript 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='classname'>" + 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,screenY=0)
window.resizeTo(screen.availWidth+2,screen.availHeight+6)
2. Defeats Frames (including Hotmail frames):
if (window != top)
top.location.href = location.href
3. Fixes a CSS Bug in Netscape Navigator:
function WM_netscapeCssFix() {
if (document.WM.WM_netscapeCssFix.initWindowWidth !=
window.innerWidth || document.WM.WM_netscapeCssFix.initWindowHeight !=
window.innerHeight) {
document.location = document.location;
}
} function WM_netscapeCssFixCheckIn() {
if ((navigator.appName == 'Netscape') && (parseInt(navigator.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.innerWidth;
document.WM.WM_netscapeCssFix.initWindowheight=window.innerHeight;
}
window.onresize = WM_netscapeCssFix;
}
} WM_netscapeCssFixCheckIn()
4. Preloads images:
if (document.images)
{
pic1= new Image(800,10);
pic1.src="images/image1.gif";
pic2= new Image(15,5);
pic2.src="images/image2.gif";
pic3= new Image(50,450);
pic3.src="images/image3.gif";
}
5. Opens a new Window with all window "furniture":
function openNewWin(url)
{
var atts =
'menubar=yes,toolbar=yes,location=yes,status=yes,scrollbars=yes,resizable=ye
s,directories=yes,menubar=1,toolbar=1,location=1,status=1,scrollbars=1,resiz
able=1,directories=1';
var newWindow = open (url,'_blank',atts);
newWindow.focus ()
}
- and is called in a HTML document (as shown in the examples below) by using
the syntax openNewWin(index.html) or openNewWin(this.href)
6. Opens a new Window in maximum size (no window "furniture"):
function openMaxWin(url)
{
var atts =
'menubar=no,toolbar=no,location=no,status=yes,scrollbars=yes,resizable=yes,m
enubar=0,toolbar=0,location=0,status=1,scrollbars=1,resizable=1';
var newWindow = open (url,'_blank',atts);
newWindow.focus ()
}
7. Opens a small pop-up type Window:
function openPopWin(url)
{
var atts =
'top=0,left=0,menubar=no,toolbar=no,location=no,status=no,scrollbars=no,resi
zable=no,screenX=0,screenY=0,menubar=0,toolbar=0,location=0,status=0,scrollb
ars=0,resizable=0,width=200,height=250';
var newWindow = open (url,'_blank',atts);
newWindow.focus ()
}
8. Opens a new Window in fullscreen:
function fullscreen(url)
{
w = screen.availWidth-10;
h = screen.availHeight-20;
features = "width="+w+",height="+h;
features += ",left=0,top=0,screenX=0,screenY=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,location=no,status=
yes,scrollbars=yes,resizable=yes');return false;">Click here</a>
10. Returns the date the page was last updated:
function lastupdated()
{
lastmod = new Date(document.lastModified);
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.lastModified).getDate();
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.getMonth()+1] + " " + lastmod.getDate() + ", "
+ year;
return updtime;
}
document.write("<font class='updatetime'>" + lastupdated() + "</font>");
11. Ways to call a function in a HTML page:
(a) Using onLoad event handler:
<body onLoad="functionName()">
(b) Using onClick event handler:
<a href="filename.htm" onClick="functionName();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.reload()">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="openNewWin(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=JavaScript 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='classname'>" + functionName() + "</font>");