Other than JavaScript 411, links to a variety of JavaScript pages on the Web can be found at Andrew Wooldridge's JavaScript Index site. If you have found your way here, I assume that you've already tried to find the information you need from Netscape's Official JavaScript Docs. They have been working hard lately to update their docs and have even posted the entire JavaScript guide in zipped format for offline viewing. I highly recommend that you get it now because when its finished there might be a charge for it in book form.
----------
<HTML>
<HEAD>
<TITLE>Example</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM OLD BROWSERS
code goes here
// --> STOP HIDING
</SCRIPT>
</HEAD>
<BODY>
HTML goes here
</BODY> </HTML>
------------
function f() { x = 3; ... }
^^^^^
where x is unbound when f's definition is parsed. The bugs may lead to
wrong answers, but should not crash you (I think).
You should say what you mean here. If the intent is for x to be local, you must use var before it. To avoid trouble, you may choose a style where you always use 'var' to introduce new variables whereever you need them. If you are writing a function that wants local variables (or needs them, if it is recursive), use var. If you want to use a global in a function, make sure there is a top-level var statement that introduces the global before the function definition:
<SCRIPT LANGAUGE="Javascript">
<!-- hide from old browsers
var x ...
function f() { x = 3; var local = ... }
// done hiding -->
</SCRIPT>
If you're looking to call a JavaScript function from within a Java Applet, you can use this call (submitted by Achille Hui):
showDocument("javascript:/**/myJSfunction()"
It appears that the /**/ is necessary to call 'myJSfunction()' instead of '/myJSfunction'. If you're interested in how Javascript reference Java in the future (Navigator 2.1), Brendan Eich wrote:
Statement of work in progress for 2.1: You will be able to get and set Java public static fields as if they were JavaScript properties of an object named after the package-qualified class name, and call Java methods that are public, provided you can reach their instances from the top-level Applet context thing. In JavaScript, you name an applet via either document.applets[myAppletIndex] or document.myApplet where a NAME attribute has been used. The reflection of public fields and methods from Java into JavaScript will be automatic and lazy.
parent.picFrame.location = pic1.gif (pic2.gif)
var mystr="This is an example of a very long sting that is just like a run-on sentence
and may break the code. Somebody suggested theres a 254 character limit for
Windows 3.1 so this example is an attempt to be at least that long. I don't
know who would simply define a string this long, but there are times when it's
necessary."
you should break it into smaller peices on sepeareate lines using the + sign to concatenate it. The fix would look like this:
var mystr="This is an example of a very long sting that is just like a run-on sentence"
+ "and may break the code. Somebody suggested theres a 254 character limit for"
+ "Windows 3.1 so this example is an attempt to be at least that long. I don't"
+ "know who would simply define a string this long, but there are times when it's"
+ "necessary."
** parent **
var myvar = ""
function setvar (value) {
myvar = "" + value; // create new string in parent context
}
** child **
parent.setvar("myvalue");
if (navigator.appVersion.indexOf("Win16") == -1) { // not Win16
eval (...); // evil!!!
}
else
doSomethingElse();
var newWin = window.open(myURL,'myWin')
if (navigator.appVersion.indexOf("(X11") != -1 ||
navigator.appVersion.indexOf("(Mac") != -1)
newWin = window.open(myURL,'myWin')
The trick here is to call window.open() a second time to force the load.
<FORM NAME="radioTest">
<INPUT TYPE="radio" NAME="testset" VALUE="A" onClick="">A
<INPUT TYPE="radio" NAME="testset" VALUE="B" onClick="">B
<INPUT TYPE="radio" NAME="testset" VALUE="C" onClick="">C
</FORM>
Here's proof that it works:
function validate() {
if (**test for valid input**)
return true
else return false
}
...
<FORM onSubmit="return validate()">
NOTE: you do not return false from the SUBMIT button's onClick handler! The former is
sufficient and relieves you from having to write N onClick handlers if
your form has N submit buttons -- you write an onSubmit handler once in
the <form ...> tag.
parent.myframe.myobj
parent.myframe.myfunction(mylocalvariable)
Note that when you call a function in another frame, the scope of the parameters that are passed is local to the calling frame. If you want to call a function on a element in another frame, you could use something like:
mylocalfunction(parent.otherframe.myvariable)
parent.otherframe.myfunction(parent.otherframe.myvariable)
and explicitly declare which frames you are referencing.
<FRAMESET COLSPAN="50,*">
<FRAME SRC="navigationbar.html" name="nav" NORESIZE>
<FRAME SRC="mainframe.html" name="info">
</FRAMESET>
Then you could put this code in your navigationbar.html page to manipulate the info frame:
<FORM NAME="buttonbar">
<INPUT TYPE="button" VALUE="Go Back" onClick="parent.info.history.back()">
<INPUT TYPE="button" VALUE="Go Home" onClick="parent.info.loaction="home.html">
<INPUT TYPE="button" VALUE="Go Next" onCLick="parent.info.history.forward()">
</FORM>
Alternatively, you could use the history.go(-1) and history.go(1) functions, as stated in the Netscape docs. Also look at the Reload button
Sure, you can synch framed JavaScript execution to start only when the
whole frameset is loaded, but you have to write some JavaScript to do it.
In the top-most FRAMESET's ONLOAD handler, you can set a variable or
property of that window ("self.loadDone = true"). In all descendent
frame documents, you can make their first SCRIPT tag (or if there are no
SCRIPT tags only event handlers, in their BODY or FRAMESET tag's ONLOAD
handler) call the following function:
function topWait(expr) {
if (!top.loadDone) {
// XXX expr had better not contain unescaped single quotes!
setTimeout("topWait('" + expr + "')", 500)
} else {
eval(expr)
}
}
to do whatever work (expr, a string containing JavaScript code, most likely a
call to the "main" function for that document) needs to be done only
after the whole frame tree is loaded.
There are more complicated schemes that involve code in different frame
documents being very careful to test (parent.otherframe.forms != null &&
parent.otherframe.forms.length == 2), e.g., and not look for properties
or members of such object references until the test succeeds.
var popWindow = window.open(myUrl,'myWindow')
popWindow.creator = self
Now you can reference the original window and all its properties/functions/...
from 'myWindow' with standard Javascipt syntax:
***code goes in popWindow's source***
creator.document.myform.myfield.value = aNewValue;
Be sure you're aware of the known bug with window.open().
<A HREF="mypage.html"" TARGET="_top>Go to my non-framed page</A>
function initArray() {
this.length = initArray.arguments.length
for (var i = 0; i < this.length; i++)
this[i+1] = initArray.arguments[i]
}
var a = new initArray(1, true, "three")
This would create an array 'a' with the following properties:
a.length = 3
a[1] = 1
a[2] = true
a[3] = three
function clock() {
new_date = new Date()
window.document.form1.f_clock.value = new_date.getSeconds()
id = setTimeout("clock()",1000)
}
<BODY onLoad="clock()">
In 2.0, the quoted expressions passed to eval() and setTimeout() are compiled into bytecode and retained for the life of the current document. So if you use setTimeout to arm another timeout, your application will bloat navigator over time. This bug will be fixed in 2.1.
Major plugin support is coming in 2.1, but in the mean time,
you can use this function to test for equipped plugins:
function probePlugIn(mimeType) {
var havePlugIn = false
var tiny = window.open("", "teensy", "width=1,height=1")
if (tiny != null) {
if (tiny.document.open(mimeType) != null)
havePlugIn = true
tiny.close()
}
return havePlugIn
}
var haveShockWavePlugIn = probePlugIn("application/x-director")
/be
NOTE: this suggestion will cause the client to be prompted (whether
to 'Pick an App', 'Save', or 'Cancel') if they do not have the plug-in.
This message was posted as a means of reference for others to think about.
| Visitor #3539 |
| © 1996 Frequency Graphics |