Solved

Why is my variable not initialised?

Posted on 2004-08-02
2
181 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

820 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