You are on page 1of 4

Introduction to JavaScript - Part 6

1996 Copyright by Stefan Koch. All rights reserved.


Loading two pages with one mose clic!
I receive a lot of mails about the problem how two pages can be loaded with only one
mouse click. Basically there are three different solutions to this problem. The first
possiblity is to create a button which calls a function when the user presses it. This
function loads the two pages into different frames or opens new windows. This is not
too difficult if you have a look at our examples in the other parts of this introduction.
We have all elements we already need.
We create three frames. The first one is used for the button. The first html-page is just
needed for openening the frames and giving names to them. This is nothing new since
we used this techniue in part ! in order to work with frames. I"ll show it to you
anyway. #I don"t know whether this happens to you as well- but everytime an author of
a computer book thinks that something hasn"t got to be printed because it is so easy -
this is exactly the part I"m having trouble with. $o here we go%&
frames2.htm
<HTML>
<HEAD>
<TITLE>Frames</TITLE>
</HEAD>
<FRAMESET COLS="295,*" >
<FRAMESET ROWS="1!,*" >
<FRAME SRC=""#a$%&#'(% m" )AME="*r1" >
</FRAMESET>
<FRAMESET ROWS="+5!,25!" >
<FRAME SRC=",e""'(% m" )AME="*r2" >
<FRAME SRC=",e""'(% m" )AME="*r-" >
</FRAMESET>
</FRAMESET>
</HTML>
loadtwo.htm is loaded to the first frame. This is the frame with the button.
loadtwo.htm
<HTML>
<HEAD>
<s,r. /% "a012a1e="3a4aS,r./%" >
' - (
<5 6 6 H.$.01
*20,%.#0 "#a$%&#7/a1e2, /a1e-8 9
/are0%'*r2'"#,a%.#0'(re* =/a1e2:
/are0%'*r-'"#,a%.#0'(re* =/a1e-:
;
// 6 6 >
</s,r. /% >
</HEAD>
<<OD=>
<FORM )AME=">2% %#0s" >
<I)?@T T=?E=">2%%#0" AAL@E="2 .0 a r#&"
#0C".,B="" #a$%&#7C *r%es%1'(% mC , C*r%es%2'(% mC 8">
</FORM>
</<OD=>
</HTML>
The function loadtwo#& is called when the button is pressed. Two strings are passed to
the function. If you look at the loadtwo#& function you see that the second frame fr)
loads the page that is defined by the first string in the function call. If you have
different buttons opening different pages you can reuse this function. *ou only have
to pass the different +,-s #addresses& of the pages.
The second techniue uses hyperlinks. $ome examples floating around the Internet
have something like this% .a href/0yourlink.htm on1-ick/0yourfunction#&02
This techniue seems not to work on all platforms. $o you better don"t use it. I"m not
sure if it is supposed to work at all - but we don"t have to worry because there is
another method how we can implement this. We can call a 3ava$cript- function the
following way%
<a (re* ="D a4as,r. /%E mF*20,%. #078" >MF L.0B</a>
This is really easy and seems to work fine on all browsers. *ou just have to write
javascript% and then the name of your function as your "link". If you call the "loadtwo#&"-
function from the example above you can update two frames with one single click on
a hyperlink. *ou should always use this techniue if you want to call a function with a
click on a link.
The third techniue for loading two pages on one mouse click can be combined either
with buttons or normal hyperlinks. We could do this with the second techniue shown
above but this approach here might be more appropriate sometimes. What we can do
is to load one 4T5-- page to one frame. This is done by
<a (re* ="1e%*r2'(% m" %ar1e%"*r2" >C" . ,B (ere5 </ a>
We know this already. What we do now is to add a on-oad property to the loaded file.
The getfr).htm file could look like this%
' - )
<HTML>
<<OD= #0L#a$="/are0%'*r-'"#,a%. #0'(re* =C 1e% *r-'(% mC : re%2r0
%r2e:">
>"a >"a >"a
</>#$F>
</(% m" >
6f course you have to add this to every document that is loaded into the second frame.
Creating a new window when a lin! is ta!en
7nother often seen problem is how new pages can be loaded to a new window. The
window shall pop up when the user clicks on a link. *ou just have to add the target-
property to your tag again. This looks like this%
<a (re* ="1#a0F&(ere'(% m" " %ar1e% ="Res#2r,e W.0$#&" >G#5 </ a>
"perators
8ow we will have a look at different operators you might use in 3ava$cript. 6perators
are a powerful techniue to shorten and improve your script. 9or example you want to
test if a variable x is larger than ! and you want to see if it is smaller than (:. *ou
might want to write it this way%
.* 7H>-8
.* 7H<18
$#a0F%(. 0178:
The function doanything#& is called if x2! and x.(:. There is a faster way to write this
though%
.* 7H>- II H<18 $#a0F%(. 0178:
The ;; is called 78<- operator. Then there is an 6,- operator. *ou can use this for
example if you want to check if one variable x is eual = or another variable y is eual
(>%
.* 7H==5 JJ F==1+8 $#a0F%(. 0178:
The function doanything#& is called when x//= or y//(>. It is also called if both
comparisons are true. 1omparisons are done via the // operator in 3ava$cript #6f
course there are also .?2?./ and 2/&. If you know 1@1AA? this is the same. 7 single /
is used to store a value in a variable. #If you know Bascal this may be a little bit
confusing. 7ssigning a value to a variable is done in Bascal via %/ and comparisons
' - !
with a single /&
If you want to see if a variable does not eual a certain number? this might get a little
bit complicated without operators. This is done with a simple C/. $o this would look
like this for example% x C/ (>.
There are many more interesting operators which can make your programs more
efficiently. -ook at the documentation provided by 8etscape to get a full overview of
all operators you can use in 3ava$cript.
Last changed: 11.May'96
1996 by Stefan Koch
' - D

You might also like