Solved

Why is my variable not initialised?

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

920 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

14 Experts available now in Live!

Get 1:1 Help Now