Class Notes


Windows

Opening Windows using Javascript

Pop-up windows are used when you want to display something but don't want to leave the page.

EXAMPLE:
      <a href="#" onClick="window.open('trivia.html', 'triviaWnd', 'width=370,height=240,status=1'); return false;">click here</a>

a href equals a # because you want to stay on the same page.

onClick is the event handler. When the user clicks, it opens a window with the specifications listed.

window.open is a function that controls the event (opens a new browser window) and contains the event specifications, including what html page to load into the window (trivia.html), the window name (triviaWnd), size of window (370x240), and its status bar (1 = yes).


• The first parameter, 'trivia.html' is the URL of the file to load in the new window. Note that it doesn't have to be an HTML file. It could also be an image file or anything the browser can display.


• The second parameter, 'triviaWnd' is the name of the new window - the name can be anything that isn't a Javascript keyword, but must it must start with a letter, $, or underscore _ . Windows need to be named, so when they are called by the script, the browser knows which window your code is trying to communicate with. This is necessary to close the window later.


      - If you want information to load in the same window, you use the same window name in your links.
       - If you want multiple windows then you need to use unique names for each window created.

• The third parameter determines the dimensions and appearance of the window. We have specified a window of size 370x240 with a visible status bar. Note that it is in single quotes and the quotes end after the last parameter.

•The appearance of the window can be controlled by changing the value of the third parameter. The commonly used options are:


NOTE: Some of the parameters below do not work in all browsers
      height - Height of the new window in pixels (eg: height=100)
      width - Width of the new window in pixels (eg: width=200)
      top - Number of pixels from top of screen (eg: top=100)
      left - Number of pixels from left of screen (eg: left=200)
      directories - Specifies whether to display the browser directory buttons like "What's Cool" etc. (eg: directories=yes)
      hotkeys - Enables or disables various hotkeys (eg: hotkeys=no)
      location - Specifies whether the "Location" box is to be displayed (eg: location=yes)
      menubar - Specifies whether the new window should have a menu bar (eg: menubar=yes)
      resizable - Specifies whether the user is allowed to resize the new window (eg: resizable=no)
      scrollbars - Specifies whether the new window can have scroll bars if necessary (eg: scrollbars=yes)
      toolbar - Specifies whether the new window has a toolbar (eg: toolbar=yes)

NOTE: Instead of yes/no values, you may also use 1 or a 0. i.e., you may write toolbar=1 instead of toolbar=yes.




Samples of opening windows with different functions:


1) Basic window. No menu, no toolbar, no status bar, not even scrollbars:


EX:
      <a href="#" onClick="window.open('myHTML.html', 'newWnd', 'width=500,height=400'); return false;">Try it</a>

TEST IT


2) Window with menubar, location box, scrollbars, but not resizable:


EX: <a href="#" onClick="window.open('myHTML2.html', 'newWnd', 'width=500,height=400,menubar=yes,location=yes,scrollbars=yes,resizable=no'); return false;">Try it</a>

TEST IT


NOTE: scrollbars won't show up if they aren't necessary.


3) Window with toolbar, statusbar and scrollbars. You can resize this window if you want which wasn't possible with above sample windows:


EX: <a href="#" onClick="window.open('myHTML3.html', 'newWnd', 'width=500,height=400,toolbar=1,status=1,scrollbars=yes,resizable=1'); return false;">Try it</a>

TEST IT



Closing Windows

Place the following code in the file displayed in the pop-up window, not the main file that opens the window.


1) Using window.close to call the name of the window:


EX: <a href="#" onClick="window.close('newWnd'); return false;">Close this window</a>

TEST IT


Opening Windows at Specific Screen Coordinates

<sample-click here>


In an a href tag:

<a href="#" onClick="window.open('filename.html', 'winName', 'width=300, height=300, top=200, left=150')">open it</a>


top = number of pixels from top of screen, ex: top=200
left = number of pixels from left of screen, ex: left=150




Opening Multiple Windows with One Link

<sample-click here>

You will NEED 4 HTML files (testR2.html, testR2_a.html, testR2_b.htm, ltestR2_c.html)

In HEAD of testR2.html:


<script type="text/javascript">

             var myWins = new Array("testR2_a","testR2_b","testR2_c");

             function newWindows() {
                   for(var i in myWins) {
                        var fileName = myWins[i] + ".html";
                        var winName = myWins[i] + "Win";
                        var multiWindow = window.open(fileName,winName,"width=140,height=160");
                  }
            }
</script>

Comments:
//testR2_a, testR2_b and testR2_c are your html file names minus the extension.
//repeats as many times as there are files in myWins - 3.
//adds extension to complete the file name.
//adds WIN to create unique names for each window.
//calls the window.open() with the current filename and current windowname.

In BODY of testR2.html:


<body>
      <a href="javascript:newWindows()">Click here to see windows</a>

</body>


If we wanted to expand on this and add a resize and position that uses the increment variable i, you might use this code:

<head>
<script type="text/javascript">
    var myWins = new Array("testR2_a","testR2_b","testR2_c");
    
    function newWindows() {
      for(var i in myWins) { 
         var fileName = myWins[i] + ".html";
         var winName = myWins[i] + "Win";
  
         var theWidth = (i+1) * 100;
         var theHeight = (i+1) * 100;
         var theTop = i*20;
         var theThirdParameter = "width="+theWidth+",height="+theHeight+",top="+theTop;
  
         var multiWindow = window.open(fileName,winName,theThirdParameter); 
      }
   }
</script> 
</head>

<body>
    <a href="javascript:newWindows()">Click here to see windows</a>
</body>

Closing multiple windows with one click

First we need to open the windows using different variables:

Notice the use of the self.focus() - this makes the window that opened the rest of them, the main window


<head>
<script type="text/javascript">
  function newWindows() {
win1 = window.open("myWin1.html","winName1","width=200,height=200,top=100");
win2 = window.open("myWin2.html","winName2","width=200,height=200,top=300");
win3 = window.open("myWin3.html","winName3","width=200,height=200,top=500");
self.focus(); }
function closeWindows() { win1.close(); win2.close(); win3.close(); } </script> </head> <body> <a href="javascript:newWindows()">Open windows with different variables</a> <a href="javascript:closeWindows()">Close all the windows</a> </body>

TEST IT


In each of the other files add this: (change the self.moveTo

<html>
      <head>

            <style type="text/css">
                  .style1 {
                        font-size: 14px;
                        font-family: Arial, Helvetica, sans-serif;
                        color: #330033;
                  }
            </style>


            <script language="javascript" type="text/javascript">
                  self.moveTo(100, 100);
            </script>

      </head>

      <body style="background-color:#6633FF;">
                  <span class="style1">hello!</span>
      </body>
</html>

 

//the self.moveTo will move the window to the x and y position specified


This code opens two windows using a TimeOut then closes them when you click the link.

<script type="text/javascript">

   function openLotsOfWindows() {
       setTimeout("win1 = window.open('myWin1.html','mywin1',
                'height=200,width=200,top=200,left=200')", 1000);
       setTimeout("win2 = window.open('myWin2.html','mywin2',
                'height=200,width=200,top=300,left=50')", 2000); 
   }

   function closeLotsOfWindows() {
       win1.close();
       win2.close();
   }
</script>
</head>
                
<body>
    <a href="#" onMouseOver="openLotsOfWindows()">Open windows</a> <br />
<br />
<br />
    <a href="#" onClick="closeLotsOfWindows()">Close Windows</a>
</body>

TEST IT


Javascript time & timing


What can you do with Javascript and time or timing?

Intervals: Timers that continue to run at a set "interval" until stopped manually.

Timeouts: Timers that run once and stop - like a kitchen timer.

Examples:

Step Through the PNGs


Click the image to go to start the animation. Roll out of the image to stop the animation.

<head>
      <script type="text/javascript">
            var myImages = new Array("../images/img1.png",
                        "../images/img2.png",
                        "../images/img3.png",
                        "../images/img4.png",
                        "../images/img5.png",
                        "../images/img6.png",
                        "../images/img7.png",
                        "../images/img8.png");
            var myInterval = "";
            var currentImg = 0;

            function animate() {
                  clearInterval(myInterval);
                  setInterval('nextImage()', 100);
            }

            function nextImage() {
                  currentImg++;
                  if (currentImg == myImages.length) {
                        currentImg = 0;
                  }
                  document.getElementById('myPNG').src = myImages[currentImg];
            }

            function stopAnimate() {
                     clearInterval(myInterval);
            }

      </script>
</head>

<body>
      <img id="myPNG" src="../images/img1.png" onclick="animate();" onmouseout="stopAnimate();">
</body>

To move a window around the screen:

This code must be in the window you open - it moves itself around

Notes:
- pos is used to hold the position of the window
- availWidth property returns the width of the visitor's screen, in pixels, minus interface features like the Windows Taskbar
- window.onload means that when the window loads it will call this function

 
<head>
<style type="text/css">
#myblock {
background-color:#C00;
width:200px;
height:200px;
}
</style> <SCRIPT LANGUAGE="JavaScript"> var theTop = 100; var WidthOfWindow = 150; function startWinMove() {
pos = 0;
theScreenWidth = screen.availWidth;
window.moveTo(pos, theTop);
timerID = setInterval("moveWindow()", 50);
}
function moveWindow() {
if (pos <= 0) {
inc = 5;
}
if ((pos + widthOfWindow) > theScreenWidth) {
inc = -5;
} pos = pos + inc; window.moveTo(pos, theTop); } window.onload = startWinMove(); </SCRIPT> </head> <body> <div id="myblock"></div> </body>

TEST IT - look at the sources code