Let's see an example:
<HTML><HEAD><TITLE>Show Date</TITLE></HEAD> <BODY> <SCRIPT LANGUAGE="JavaScript"> var x= new Date(); document.write (x); </SCRIPT> </BODY></HTML>
To activate a Date Object, you can do this: var x=new Date()
. Whenever you want to create an instance of the date object, use this important word: new followed by the object name().
var banTime= new Date() var ss=banTime.getHours() if (ss<=12) document.write("<img src='banner1.gif'>") else document.write("<img src='banner2.gif'>")
Methods | ||
getDate getTime getTimezoneOffset |
getDay getMonth getYear |
getSeconds getMinutes getHours |
<form> <input type="button" value="Click here to see" onclick="window.open('test.htm')"> </form>
You can replace test.htm
with any URL, for example, with http://www.yahoo.com
.
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:
width | height | toolbar |
location | directories | status |
scrollbars | resizable | menubar |
window.location.reload()
<form> <input type="button" value="Close Window" onClick="window.close()"> </form> <a href="javascript:window.close()">Close Window</a>
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> <!-- function ss() { var ok=confirm('Click "OK" to go to yahoo, "CANCEL" to go to hotmail') if (ok) location="http://www.yahoo.com" else location="http://www.hotmail.com" } //--> </script>
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>
"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 test.htm
):
<html> <head> <title></title> <script> function remote(url){ window.opener.location=url } </script> </head> <body> <p><a href="#" onClick="remote('file1.htm')">File 1</a></p> <p><a href="#" onClick="remote('file2.htm')">File 2</a></p> </body> </html>
Try it now!
One 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 "parent.frameName.location"
to access another frame. "parent" standards for the parent frame containing the frameset code.