File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes java script problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "java script problem" Watch "java script problem" New topic

java script problem

kanumuri koti

Joined: Apr 05, 2006
Posts: 6
Hi everyone,

I have a data grid and 2 buttons on a page, when a user cancels the window, i want to displat a popup asking the whethere they are sure they want to cancel.

i currently have

function SaveChanges()
event.returnValue = "You will loose all unsaved changes, please update details before closing the window.";

i call this function on the body like

<body bgColor="#ffffff" MS_POSITIONING="GridLayout" onbeforeunload="SaveChanges()">

Problem is, this message comes up even when clicking the buttons on the page, which is wrong, i want it to happen only when the user closes/cancels the window. Please help

[ December 13, 2006: Message edited by: kanumuri koti ]
[ December 13, 2006: Message edited by: Bear Bibeault ]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
You need to add a flag to all of the events that cause a post back. (aka set a JavaScript variable onclick) In the SaveChanges you have to check to see if that flag/bit was flipped or not. Than you know if it should be shown.

You will never get the same feel as a client application.

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63852

As a favor I have removed the "very urgent" from the title. Please read this to find out why that's a really bad idea.

You could also use better subjects. Also read this.
[ December 13, 2006: Message edited by: Bear Bibeault ]

[Asking smart questions] [About Bear] [Books by Bear]
Neerav Narielwala
Ranch Hand

Joined: Dec 08, 2006
Posts: 106
You can also use the "closed" property on the window.opener object to determine whether the window that opened the window in question is still open (are you still with me?). For example:

<script type="text/javascript">
if (window.opener.closed)
alert("You killed my boss!")
alert("My boss is still alive!")

<a href="" target="_blank" rel="nofollow">Java Tutorials</a> | <a href="" target="_blank" rel="nofollow">Java Weblog</a> | <a href="" target="_blank" rel="nofollow">Computing Articles</a>
I agree. Here's the link:
subject: java script problem
It's not a secret anymore!