Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Onblur and Onfocus

Posted on 1999-07-19
3
Medium Priority
?
805 Views
Last Modified: 2008-02-26
How can I close automatically a window which has been
opened with open.window ?
0
Comment
Question by:us111
[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
3 Comments
 
LVL 7

Expert Comment

by:nettrom
ID: 1289595
open.window is Microsoft's Jscript if I remember correctly.

when you open the window in JavaScript the return value from window.open() is the new window, which you then can manipulate.  make sure you keep that return value by assigning it to a variable.  e.g:

myWindow = window.open( ... );

then you can simply do:

myWindow.close();

you mention onblur() and onfocus(), when is it you want the close to trigger?
0
 
LVL 8

Author Comment

by:us111
ID: 1289596
Do you know how this works ?

window.onblur=handler-func  defining the handler directly
window.onblur();  an explicit invocation of the handler
window.onfocus();

In fact, I wish to open a new window (y) with window.open and then
when I click into the main window (x), the window (y) closes itself
0
 
LVL 7

Accepted Solution

by:
nettrom earned 80 total points
ID: 1289597
ah... :)

<script type="text/javascript">
var myWindow;

function openWindow(myURL) {
  if(myWindow && !myWindow.closed) {
    myWindow.location = myURL;
  } else {
    myWindow = window.open('myURL', 'my_window');
  }
return false;
}

function closeWindow() {
  if(myWindow && !myWindow.closed) {
  myWindow.close()
  }
}
</script>

call the script by:

<a href="link_for_those_without_javascript.html"
  onclick="return openWindow('my_url.html');">link text</a>

and then use either:

window.onfocus = closeWindow;

or

<body onfocus="closeWindow()">

the onfocus-attribute to BODY doesn't validate as HTML 4.0 so if you want it to validate you'll have to use your own custom DTD and a DOCTYPE that points to it (but it works in both IE4/5 & Netscape 4.x).
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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…

715 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