|
Methods
Smile for goodness sake!! This is FUN!
window.alert() You've already used this method, but we've never really discussed it in depth. As you know, using this method displays a JavaScript alert box with a message in it and an 'OK' button. Here's an example of the syntax:
This is a good one to start with because the syntax is so simple. The first part - in red - tells the browser which object you are using (in this case 'window'). The next part - in purple is the method we are using with or on that object. All method options (parameters)are set within the parentheses that immediately follow the method name. The part in green is the value that the alert box will display. This value can be assigned in 2 ways.
|
|
<head> <script> <!-- function openit(){ winda = window.open("http://www.useractive.com/udidit.html","newone","scrollbars,toolbar=no,location=no,directories=no,status=no,menubar=yes,resizable=yes,width=620,height=400"); } //--> </script> </head> <body> <form> <input type=button onClick=openit(); value="open a window"> </form> </body> |
|
WARNING: DO NOT PUT RETURN CHARACTERS IN BETWEEN THE QUOTES OR IT WON'T WORK. That's why that line up there is so long. Actually put an enter (or return) after location=no, for instance and see for yourself that your script won't work. You're job now is to make that example much more interesting!!! window.focus() If for some reason after the first click the new window you opened in the last example gets BEHIND the original one (i.e. it gets blurred) then the next time you click on the "open a window" button, the window will already be open but behind the first one. For this reason when you open a window it is always a good idea to focus it. Well just add one line to the last example. In the new example below notice the use of the local name to call the new window. |
|
Type the following into CodeRunner below:
|
document.open() document.write() document.close() Okay, here is the plan....we are going to cover three methods at once here. That's because they don't make much sense apart. Suppose your mean old boss wants you to open a window, but doesn't want to open a new page from the server. He just wants a quick message put up. Just pretend, okay?! We are going to write a page from another page. here is the example. NOTICE that there is no "URL" anymore in the window.open() method. |
|
Type the following into CodeRunner below:
|
|
That's it. That's what you do. You can put any HTML between the quotes in
the write method. Be careful with return characters however!
Right now you should put a return after one of the words in IT WORKS DUDE
to see what happens. Do you understand that we have to open a document before we write to it, and then close it? You do? Could you explain it to me? Well, this is one of those things that I think is kind of unnecessary, but apparently the good people at Netscape don't agree - and who am I to tell them any different? Here's what we just did. the open() method allows us to start writing to the document. write() is self explanatory (I think). And, close() makes the browser display everything that we have just written to the document. Get It?? |