Overview of Java ScriptContents
Embedding and includingLet's first see a simple example:- <html> <head> <title>This is a JavaScript example</title> <script language="JavaScript"> <!-- document.write("Hello World!"); Usually, JavaScript code starts with the tag <html> <head></head> <body> <script> .....// The code embedded in the <body> tags. Why do we place JavaScript code inside comment fields <script language="JavaScript1.2"> You can use another attribute SRC to include an external file containing JavaScript code: <script language="JavaScript" src="hello.js"></script> For example, shown below is the code of the external file document.write("Hello World!") The external file is simply a text file containing JavaScript code with the file name extension ".js". Note:
write and writelnIn order to output text in JavaScript you must use <HTML> <HEAD> <TITLE> Welcome to my site</TITLE></HEAD> <BODY> <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- Note: the document object Document objectThe document object is one of the most important objects of JavaScript. Shown below is a very simple JavaScript code: document.write("Hi there.") In this code, lastModifiedYou can always include the last update date on your page by using the following code:<script language="JavaScript"> document.write("This page created by John N. Last update:" + document.lastModified); All you need to do here is use the bgColor and fgColorLets try playing around withbgColor
and fgColor:
<script> document.bgColor="black" document.fgColor="#336699" </script> Message BoxalertThere are three message boxes:alert, confirm, and
prompt. Let's look at the first one:
<body> <script> window.alert("Welcome to my site!") You can put whatever you want inside the quotation marks. confirmAn example for confirm box:window.confirm("Are you sure you want to quit?") promptPrompt box is used to allow a user to enter something according the promotion:window.prompt("please enter user name") In all our examples above, we wrote the box methods as alert() Variables and ConditionsLet's see an example: <script> var x=window.confirm("Are you sure you want to quit") There are several concepts that we should know. First of all, <script> var y=window.prompt("please enter your name") Another example: <html><head> <script> var x=confirm("Are you sure you want to quit?") If you click "cancel", it will take you to yahoo, and clicking
ok will continue with the loading of the current page "Welcome
to my website!". Note: FunctionFunctions are chunks of code.Let's create a simple function: function test() Note that if only this were within your function test() Last line Event handlerWhat are event handlers? They can be considered as triggers that execute JavaScript when something happens, such as click or move your mouse over a link, submit a form etc. onClickonClick handlers execute something only when users
click on buttons, links, etc. Let's see an example:
<script> function ss() { alert("Thank you!") The function onLoadThe onload event handler is used to call the execution of JavaScript after loading:<body onload="ss()"> <frameset onload="ss()"> <img src="whatever.gif" onload="ss()"> onMouseover,onMouseoutThese handlers are used exclusively with links.<a href="#" onMouseOver="document.write('Hi, nice to see you!">Over Here!</a> <a href="#" onMouseOut="alert('Good try!')">Get Out Here!</a> onUnloadonunload executes JavaScript while someone leaves
the page. For example to thank users.
<body onunload="alert('Thank you for visiting us. See you soon')"> Handle multiple actionsHow do you have an event handler call multiple functions/statements? That's simple. You just need to embed the functions inside the event handler as usual, but separate each of them using a semicolon:<form> <input type="button" value="Click here!" onClick="alert('Thanks FormLet's say you have a form like this: <form name="aa"> <input type="text" size="10" value="" name="bb"><br> <input type="button" value="Click Here"onclick="alert(document.aa.bb.value)"> </form> Notice that we gave the names to the form and the element. So JavaScript can gain access to them. onBlurIf you want to get information from users and want to check each element (ie: user name, password, email) individually, and alert the user to correct the wrong input before moving on, you can useonBlur. Let's see how onblur works:
<html><head><script> function emailchk() <form If you enter an email address without the alert("It seems you entered an invalid email address.") What's <script> <!-- <form name="login" onsubmit="return validate()"> <input type="text" size="20" name="userName"> <input type="text" size="20" name="password"> <input type="submit" name="submit" value="Submit"> </form> Note: Protect a file by using LoginLet's try an example<html><head> <SCRIPT Language="JavaScript"> function checkLogin(x) </head><body> <form> <p>UserID:<input type="text" name="id"></p> <p>Password:<input type="password" name="pass"></p> <p><input type="button" value="Login" onClick="checkLogin(this.form)"></p> </form> </body></html>
LinkIn most cases, a form can be repaced by a link: <a href="JavaScript:window.location.reload()">Click to reload!</a> More examples: <a href="#" onClick="alert('Hello, world!')">Click me to say Hello</a><br> <a href="#" onMouseOver="location='main.htm'">Mouse over to see Main Page</a> DateLet's see an example: <HTML><HEAD><TITLE>Show To activate a Date Object, you can do this: Dynamically display different pagesYou can display different pages according to the different time. Here is an example:var banTime= new Date()
WindowOpen a windowTo open a window, simply use the method "window.open()":<form> <input type="button" value="Click here to see" onclick="window.open('test.htm')"> </form> You can replace Size, toolbar, menubar, scrollbars, location, statusLet's add some of attributes to the above script to control the size of the window, and show: toolbar, scrollbars etc. The syntax to add attributes is:open("URL","name","attributes") For example: <form> <input type="button" value="Click here to see" onclick="window.open('page2.htm','win1','width=200,height=200,menubar')"> </form> Another example with no attributes turned on, except the size changed: <form> <input type="button" value="Click here to see" onclick="window.open('page2.htm','win1','width=200,height=200')"> </form> Here is the complete list of attributes you can add:
ReloadTo reload a window, use this method:window.location.reload() Close WindowYour can use one of the codes shown below:<form> <input type="button" value="Close Window" onClick="window.close()"> </form> <a href="javascript:window.close()">Close Window</a> LoadingThe basic syntax when loading new content into a window is:window.location="test.htm" This is the same as <a href="test.htm>Try this </a> Let's provide an example, where a confirm box will allow users to choose between going to two places: <script> <!-- Remote Control WindowLet's say you have opened a new window from the current window. After that, you will wonder how to make a control between the two windows. To do this, we need to first give a name to the window.Look at below:aa=window.open('test.htm','','width=200,height=200') By giving this window a name "aa", it will give you access to anything that's inside this window from other windows. Whenever we want to access anything that's inside this newly opened window, for example, to write to this window, we would do this: aa.document.write("This is a test."). Now, let's see an example of how to change the background color of another window: <html><head><title></title></head> <body> <form> <input type="button" value="Open another page" onClick="aa=window.open('test.htm','','width=200,height=200')"> <input type="radio" name="x" onClick="aa.document.bgColor='red'"> <input type="radio" name="x" onClick="aa.document.bgColor='green'"> <input type="radio" name="x" onClick="aa.document.bgColor='yellow'"> </form> </body></html> openerUsing"opener" property, we can access
the main window from the newly opened window.
Let's create Main page: <html> <head> <title></title> </head> <body> <form> <input type="button" value="Open another page" onClick="aa=window.open('test.htm','','width=100,height=200')"> </form> </body> </html> Then create Remote control page (in this example, that is <html> <head> <title></title> <script> function remote(url){ Try it now! FrameOne of the most popular uses of loading multiple frames is to load and change the content of more than one frame at once. Lets say we have a parent frame: <html> <frameset cols="150,*"> <frame src="page1.htm" name="frame1"> <frame src="page2.htm" name="frame2"> </frameset> </html> We can add a link in the child frame "frame1" that will change the contents of not only page1, but page2 too. Shown below is the html code for it: <html> <body> <h2>This is page 1 </h2> <a href="page3.htm" onClick="parent.frame2.location='page4.htm'">Click Here</a> </body> </html> Notice: You should use Function Pointers in JScriptUnless you're from a C/C++ background, function pointers/variables might seem like a strange concept. Basically, a function pointer is just like any other variable out there, but instead of pointing to an integer or string type, it points to the actual function. Bruce Eckel defines a function pointer as: "Once a function is compiled and loaded into the computer to be executed, it occupies a chunk of memory. That memory, and thus the function, has an address".To illustrate this, here's a basic usage example. function dostuff()
fnpointer of type dostuff. Now, obviously, this isn't one of the JScript intrinsic type. The variable
fnpointer is actually referencing the function dostuff and is used as a direct call to
dostuff() would be ( viz Function(param1, param2) ).
Here's an example where parameters are being used: function dostuff(sblah)
What you can also do is pass a function variable to a function and the function will be able to call the function this function variable points to, without much knowledge about the actual function. One thing that true OO languages would do at this point is use interfaces. JScript doesn't have these. Here's an elementary example of this function myfnvar()
Creating a pop-under windowI feel guilty writing this tutorial, but as the saying goes, "give the people what they want", or to be more precise, developers. A lot of developers would like to know how to implement pop-under windows on their site as a way to broadcast advertising. Yes it's annoying for the visitor, but proven quite effective in getting attention. Creating a pop-under window is very simple, by using JavaScript. Basically one would use the same method for creating a popup window, but then "un-focus" it. Here is the code in full force: <script> That's it! I open a window by invoking You've probably seen more sophisticated pop-under scripts that control the "frequency" of the pop-under, such as popping up only once per browser session. Instead of reinventing the wheel, I'll simply point you to a nice example of this on JavaScript Kit: http://javascriptkit.com/script/script2/popunder.shtml Have fun with popping under your windows. Using JavaScript to write to the status barSome of the simplest effects in JavaScript are also the most useful, in my opinion. One example is using JavaScript to write something to the status bar. The status bar is a lonely, empty place just waiting for you to take advantage of. You can use it to display a description of the link your mouse is currently over, show some message etc. To write to the status bar, use the JavaScript function: <script> For example, I create a link below that uses this function to a description
of the link <a href="http://www.google.com" onMouseover="writetostatus('Search the web!')"
I like this example by Website Abstraction, which gets really creative and "unscrambles" the message into view. ConclusionThe key to writing to the status is using the property | |||||||||||||||




http://209.85.173.104/search?q=cache:www.pageresource.com/jscript/jwinopen.htm
http://209.85.173.104/search?q=cache:www.pageresource.com/jscript/index.html