Solved

Why is my variable not initialised?

Posted on 2004-08-02
2
185 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…
Suggested Courses

632 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