reptile7's JavaScript blog
Wednesday, August 19, 2009
 
Sans Javamax
Blog Entry #154

A little over a year ago, we discussed the width-and-height maximization and minimization of primary browser windows in the course of going through HTML Goodies' aptly titled "Minimize/Maximize a Browser Window" tutorial. HTML Goodies also offers two tutorials with scripts for maximizing window.open( )-opened windows:
(1) "The Same Size"
(2) "So, You Want A FullScreen Browser, Huh?"
We'll take on "The Same Size" in today's post.

The "Same Size" script

Joe provides no deconstruction for the "Same Size" script but pronounces it quite brilliant. As we'll see below, it's actually a sloppily written script, although it does sport a unique and somewhat bizarre feature, namely, it contains an if block whose statements are Java (not JavaScript) expressions. The script is reproduced below as it appears on Joe's "Grab the Script" page:

<SCRIPT>
if(navigator.javaEnabled()==true)
{ w=java.awt.Toolkit.getDefaultToolkit().getScreenSize().width= java.awt.Toolkit.getDefaultToolkit().getScreenSize().height}
else {w=629; h=469}
properies="width="+w+",height="+h;
window.open("index.html","somewindowname", "toolbar=nyes,location=yes,directories=yes,status=yes,menubar=yes,
scrollbars=yes,copyhistory=no")
</SCRIPT>


"The Same Size" is the only "Opening New Windows With JavaScript" tutorial for which Joe does not provide a demo. After I cleaned up the above script

<script type="">
if (navigator.javaEnabled( )) {
w = java.awt.Toolkit.getDefaultToolkit( ).getScreenSize( ).width;
h = java.awt.Toolkit.getDefaultToolkit( ).getScreenSize( ).height; }
else { w = 629; h = 469; }
properies = "width=" + w + ",height=" + h;
window.open("index2.html", "somewindowname", "toolbar,location,directories,status,menubar,scrollbars," + properies)
</script>


I got it to open a new window (but not a maximized window) with Opera but with no other browsers on my computer.

Deconstruction

Let's begin with the script element start-tag, which in the original script is not equipped with a type attribute nor even a language attribute. I don't know if specifying a type="text/javascript" or language="javascript" script attribute would prevent the reading of the if Java statements in some platform situations. At the least we should add a type attribute set to an empty string for validation purposes (type="text/javascript" worked OK with Opera, BTW).

Next we have the aforementioned Java-holding if block, whose condition accordingly checks if the user's browser is Java-enabled. We learned in Blog Entry #142 that a true return for the javaEnabled( ) method of the navigator object does not guarantee that the user's system will in practice execute Java code, but for a moment let's assume that a suitable 'Java Runtime Environment' is in fact installed on the user's computer.

My meager knowledge of Java is not up to the task of providing a detailed explanation of the expressions in the if block, but with the aid of the JavaTM Platform, Standard Edition 6 API Specification, let me take a stab at giving you a 'tourist's guide' to what's going on:

• The expressions begin with a reference to the java.awt "package", which is documented by Sun here - awt stands for "abstract window toolkit".

• The java.awt package contains a Toolkit class, which has (among many other methods) a getDefaultTookit( ) method and a getScreenSize( ) method.

java.awt.Tookit.getDefaultTookit( ) returns the user's "default tookit", which approximately (not so approximately?) corresponds to the user's GUI as a whole.

• The getScreenSize( ) method returns a Dimension object having width and height "fields" (the Java term for properties/attributes). getScreenSize( ).width and getScreenSize( ).height are functionally equivalent to the JavaScript expressions screen.width and screen.height, respectively, i.e., they return the width (more precisely, the horizontal resolution) and height (vertical resolution) in pixels of the user's monitor screen.

The java.awt.Toolkit.getDefaultToolkit( ).getScreenSize( ).width and java.awt.Toolkit.getDefaultToolkit( ).getScreenSize( ).height returns are meant to be assigned to w and h variables, respectively. But go look at the original script, which assigns java.awt.Toolkit.getDefaultToolkit( ).getScreenSize( ).height to java.awt.Toolkit.getDefaultToolkit( ).getScreenSize( ).width*, which in turn is assigned to w. Although a = b = c expressions are in fact 'legal' in both Java and JavaScript - assignment occurs in the normal right-to-left manner, with the value of c being transferred first to b and then to a - we obviously don't want to be doing this here.
(*In analogy to JavaScript's screen.width and screen.height properties, the width and height fields for the getScreenSize( ) return should be read-only, but now that I think about it, there's no reason why they couldn't be writable - it should in theory be possible to change the user's screen resolution on the fly if doing so were desirable for a given Web page.)

Note that the w and h variables are not declared in the original script, which is allowed in JavaScript but a no-no in Java. Just out of curiosity, I prepended an

int w, h;

statement to the script code and reran the script; without getting into the details, the int line threw errors with all of the OS X browsers on my computer. FWIW: All of the Java primitive data type names - boolean, byte, char, etc. - are reserved as future keywords by the ECMAScript specification.

If the user has turned off Java support via the browser Preferences pane or is using a browser that doesn't support Java in the first place - situations for which navigator.javaEnabled( ) should return false - then the subsequent else block assigns 629 to w and 469 to h; evidently the script was written at a time when the standard/most common screen resolution was 800 x 600.

Following the if...else statement is a line that concatenates width=, w, ,height=, and h, and then assigns the resulting string to a properies variable. (properties? Whatever.)

We finally come to the window.open( ) command. Joe set the command's strUrl parameter to index.html, which is meant to be a 'dummy file name' (à la myFile.html) but is a less innocent strUrl choice than it might seem, because a file with this name will, when uploaded, supplant the current directory's normal index file. Assuming that you don't want this to happen, the new window's document should be named something else - I chose index2.html for my revised script.

Note that properies is not concatenated with the command's strWindowFeatures string in the original script - it's up to us to do that. If properies is prepended to the strWindowFeatures string, then a delineating comma should be added to the beginning of the strWindowFeatures string; conversely, if properies is appended to the strWindowFeatures string (as in my script), then a comma should be added to the end of the strWindowFeatures string.

The strWindowFeatures toolbar feature is set to "nyes" - let's assume this should be yes. The strWindowFeatures location, directories, status, menubar, and scrollbars features are also turned on, as befits a maximized window.

Curiously, the strWindowFeatures string concludes by setting a copyhistory feature to no. The copyhistory feature doesn't appear in any of Netscape's pre-DOM JavaScript specifications (which are helpfully archived here by nihonsoft LLC) and it doesn't appear on Microsoft's current window.open( ) page. My guess is that copyhistory was first implemented by Microsoft in an early version of JScript, but my attempts to locate specifications for JScript 1.0 and JScript 2.0 have not been successful.

More on copyhistory

Located via a Google search, this page defines the copyhistory feature - Copies the history listing from the opener window to the new window - and dates copyhistory's implementation to Navigator 2 and Internet Explorer 3. Navigator 2.0 'maps onto' JavaScript 1.0 (the first version of JavaScript), and therefore copyhistory should crop up in JavaScript 1.0's window.open( ) section, but it doesn't.

More recent metadata on the copyhistory feature is given by Gérard Talbot's "Complete cross-browser window.open( ) documentation" page, whose brief copyhistory entry reads:

copyhistory
Deprecated. Do not use. Always implemented in Mozilla-based browsers. There are currently no plan[s] to implement the copyhistory feature either.

Anyway, given that the script's author set copyhistory to no, it's not really worth our while to obsess about copyhistory one way or the other.

In practice

I've never seen JavaScript and Java mixed together before; you wouldn't expect such a script to work, and for the most part it doesn't. Here's what happens when I attempt to run my cleaned-up "Same Size" script with the main browsers on my computer:

• Safari throws a Can't find variable: java "ReferenceError". Equivalently, MSIE 5.2.3 for Mac OS X throws a 'java' is undefined runtime error, as does MSIE 4.5 in the SheepShaver environment.

• Firefox throws a Java class apple.awt.MyCToolkit has no public field or method named "getScreenSize" error.

• With Netscape Communicator 4.61 in the SheepShaver environment, SheepShaver crashes.

As noted above, Opera alone runs my script. The new window has the width and height of the opener window even though the script returns correct/maximized values for w and h (1680 and 1050, respectively, as determined by a window.alert("The value of the properies variable is: " + properies); command placed prior to the window.open( ) command).

Losing the Java and maximizing the window

So, do we really need Java to generate a maximized new window? Not at all - JavaScript alone is up to the job. For Firefox and MSIE, all you need to do is directly set the new window's width and height features to screen.width and screen.height, respectively:

window.open("index2.html", "somewindowname",
"toolbar,location,directories,status,menubar,scrollbars,width=" + screen.width + ",height=" + screen.height);


For Safari, adding left=0,top=0, to the above strWindowFeatures string will do the trick:

// Can also be used with Firefox and MSIE:
window.open("index2.html", "somewindowname",
"left=0,top=0,toolbar,location,directories,status,menubar,scrollbars,width=" + screen.width + ",height=" + screen.height);


Opera's execution of my "Same Size" script suggests that we can't create a maximized window with Opera: not so. I subsequently discovered that when the strWindowFeatures location feature is enabled, Opera takes control of the new window size, position, and chrome, specifically, the new window is given the size and chrome of the opener window and is slightly offset from (to the right and lower than) the opener window, regardless of what other features are or are not set in the strWindowFeatures string. Ditch the location feature and Opera will give you a maximized window with:

window.open("index2.html", "somewindowname", "width=" + screen.width + ",height=" + screen.height);

As for a location-enabled window, Opera takes control of the position and chrome of a maximized window:
• The left and top features are automatically set to 0; setting left and top to other values has no effect.
• The window has a title bar plus a small bar immediately below the title bar indicating the document server's hostname (e.g., www.htmlgoodies.com), and that's it - there's no tab bar, address bar, navigation bar, or status bar - regardless of what other features are or are not set in the strWindowFeatures string.

Time to try it all out:

Welcome to the "Same Size" demo opener page (well, div).




What also doesn't work

Regarding the other HTML Goodies window-maximization tutorials cited at the beginning of the post:

(1) "Minimize/Maximize a Browser Window" tries to minimize/maximize a window via the writing of window object properties (innerWidth, innerHeight, screenX, and screenY) that are meant to be read-only; its code works with the Mozilla/Netscape family of browsers but not with other browsers.

(2) "So, You Want A FullScreen Browser, Huh?" relies on the strWindowFeatures fullscreen feature to create a maximized window. This tutorial deserves its own entry, but for now let me say that fullscreen is supported by iCab 3.0.5 in the SheepShaver environment but not by any other browser on my computer.

The next "Opening New Windows With JavaScript" tutorial is "Remote Control JavaScript", which we'll look over in the following post.

reptile7

Comments: Post a Comment

<< Home

Powered by Blogger

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)