onbeforeunload error

D

David

I am using a process to warn users when they leave a page that there have
been changes made. I took the example directly from Microsoft example I
found in searching for help on the event onbeforeunload. I have the event
inside the BODY element in my aspx page and it seems to work just fine.
However, I still get a flash of a page error in the lower left corner of my
browser and the Visual Studio 2005 "souce" page shows the event name
underlined with mouseover message "Validation (XHTML 1.0 Transitional):
Attribute 'onbeforeunload' is not a valid attribute of element 'body'."

Is there anything I can do to get rid of this error? Thanks.

David
 
T

Teemu Keiski

Hi,

yes it is not valid.

You can change the body tag to be server-side object

<body id="BodyTag" runat="server">

and then add the call via Attributes collection in code:

BodyTag.Attributes["onbeforeunload"] = "closeIt()";

And with ASP.NET 2.0, you can also use expando attribute mechanism to
preserve compatibility with rendered markup. Then just have <body> also with
ID and runat="server" and the relevant code

Page.ClientScript.RegisterExpandoAttribute(BodyTag.ClientID,
"onbeforeunload", "closeIt");
string script=String.Format("eval(\"{0}.onbeforeunload = \" +
{0}.onbeforeunload + \";\");",BodyTag.ClientID);
Page.ClientScript.RegisterStartupScript(this.GetType(), "evaling",
script, true);

(closeIt is function which is called when onbeforeunload fires)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top