Solved

Why is my variable not initialised?

Posted on 2004-08-02
2
176 Views
Last Modified: 2006-11-17
Take a look at the following code. It creates a page with a button, which when clicked opens a new window. That bit is working.

The onFocus event triggers when the original page gets the focus, and displays an alert box. That is working too.

However, the var "win" which has the reference to the new window is not initialised by the time the onFocus event fires, even though the window has been opened successfully. I'm not that used to Javascript, so I must be missing something pretty stupid. Any ideas?

<html>
<head>
<title>This is the opening page</title>
</head>

<script Language=JavaScript>

  var win = null;
  var nms = "Neil is initialised";

  function butOpenWin_onclick()
  {
    win = window.open("http://www.google.co.uk", "myWindow","resizable=yes");
  }

  function gotFocus()
  {
        alert("Got the onFocus event");
        if (!win) alert("win is not initialised");
    if (win && win.closed) alert("The window has closed");
    if (win && win.open) alert("The window is open");
  }
</script>


<body onfocus="gotFocus()">
<input type="button" value="Open" onClick="JavaScript: window.open('http://www.google.co.uk', 'newWindow')">
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
<p>Some text to click in............</p>
</body>
0
Comment
Question by:crescendo
2 Comments
 
LVL 2

Accepted Solution

by:
Michael_Cicogna earned 500 total points
ID: 11697658
Hi!

I think your problem is rather easy to solve!

You are calling
    JavaScript: window.open('http://www.google.co.uk', 'newWindow')
in the OnClick event of the button.

You should replace it with
    butOpenWin_onclick
which should work then

Michael
0
 
LVL 9

Author Comment

by:crescendo
ID: 11697690
D'oh!!

I did that for testing, then wrote the functions, and never went back. I just assumed I'd got the functions wrong!

Many many thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now