Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Modal Dialog Killer

10:59 am on Dec 7, 2012 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 20, 2004
posts: 615
votes: 0

Hello All -

I'm still pretty new at javascript but I've got a few functions that are working pretty well with one exception.

Here's the scenario:

1. User deletes an item from a page.

2. javascript function deletes the target item from the database and then reloads the remaining items from the same table.

3. a modal dialog appears notifying the user that the item has been successfully deleted. The user must click the "OK" button on the modal for it to dismiss.

All of the above works fine, except when...

The user deletes the LAST ITEM on a page window.location.href take the user to another page.

Under this circumstance, the:

- Item is deleted
- User is automatically re-directed
- Modal appears, but disappears within a few seconds without the user needing to click the ok button.

What is appearing to happen is that window.location.href is still happening when the modal is fired... but what's weird is that window.location is called BEFORE the model is called.

The actual code-calls are here:

// Redirect if needed
if(data.redirect) window.location.href = data.redirect;

// Display remaining data from database
if(data.content) {

// Fire confirmation modal dialog

Anyone got any idea on how to (if this is even the problem) make sure the re-direct has completed BEFORE the modal is called.

All guidance gratefully appreciated!
6:36 pm on Dec 7, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member drdoc is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 15, 2002
votes: 0

The problem is that the JavaScript code completes much faster than the browser takes to redirect.

A very simple fix is to make your code a complete

if (data.redirect) { 
window.location.href = data.redirect;
} else {
if (data.content) {