# Get focus after an alert pop-up

**URL:** <https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888>\
**Category:** programming\
**Created:** [September 1, 2003, 4:30pm UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888 "2003-09-01T16:30:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pom](https://avatars.discourse-cdn.com/v4/letter/p/f07891/32.png) [@pom](https://forum.kirupa.com/u/pom)\
**Post date:** [September 1, 2003, 4:30pm UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888/1 "2003-09-01T16:30:32Z")

</div>

Hi guys,  
When an illegal character comes up in a textfield, I have an alert pop-up telling me how stupid I am, and I’d like to automatically set the focus to the right textbox, but I don’t know how to detect the closing of the pop-up.

Any ideas?

pom 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2003, 5:35pm UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888/2 "2003-09-01T17:35:39Z")

</div>

I’ll assume you know how to set the focus to a textfield(because I don’t :P). You can use the onunload event of body to run Javascript. Something like this:

```php

<html>
<head>
<title>close me!</title>
<script language="JavaScript">
function traceMe(){
alert('You closed the window! How dare you...');
}
</script>
</head>
<body onunload="traceMe()">
</body>
<html>

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2003, 5:57pm UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888/3 "2003-09-01T17:57:30Z")

</div>

I think Pom’s talking about from an windows alert box. You know, the ones that only have an okay button - or what some bastards are using as the ‘new popup’ advertisements.

And, no, I cant think of a way unless you have a script that repeatedly sets focus, but then you could only enter info in one field.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2003, 5:59pm UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888/4 "2003-09-01T17:59:52Z")

</div>

njs12345, i dont think thats what Ilyas want.  
I understood he wants to detect the _window.alert_ closing. :-\

Ilyas, i dunno if thats possible, and if not, you could use the window.confirm method.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 1, 2003, 6:36pm UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888/5 "2003-09-01T18:36:43Z")

</div>

I think script processing is paused while an alert box is up, so you can just go

```auto

alert("YOU ARE A FOOL!");
//assuming that setFocus is what you use to set focus
this.setFocus("blahtext");

```

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 2, 2003, 8:57am UTC](https://forum.kirupa.com/t/get-focus-after-an-alert-pop-up/29888/6 "2003-09-02T08:57:41Z")

</div>

njs12345 \>\> Many thanks, I thought I had tried that, but apparently not hard enough, but it works:

```php
function controlForm () {
	// ...
	if ( ! checkText ('rechercher') ) {
		alert ("Invalide characters") ;
		GiveFocus("texte") ;
		return false ;
	}	
	return true ;
}
function GiveFocus(name) {
	document.forms[0].elements[name].focus();
}

```

> \*Originally posted by claudio \*  
> \*\*Ilyas, i dunno if thats possible, and if not, you could use the window.confirm method. \*\*  
> I’ll look in to that too, thanks Claudio 🙂
