Javascript closes IE

M

m miller

I have the following javascript in my html:
<script language="javascript">
function setfocus()
{
if (document.body.style.cursor="default")
{
document.body.style.cursor="wait";
document.Form1.txtPartno.focus();
document.Form1.txtPartno.select();
}
else
{
document.body.style.cursor="default";
document.Form1.txtPartno.focus();
document.Form1.txtPartno.select();
}
}
</script>

In my codebehind I have: Me.btnFind.Attributes.Add("onclick", "setfocus()")

It seems to run fine for at least 2 to 3 clicks and then issues a send error
report dialogue saying:
'Internet Explorer has encountered a problem and needs to close.',
crashing, of course, IE.

I have IE 6 and .NET 2003. I have encountered this on Windows XP Pro,
Windows 2000 Pro and Windows 2000 Server.
I am not a javascript programmer so I have no clue as to why this is
happening.

TIA,
Marc Miller
 
S

Scott M.

Try renaming the function to something other than setfocus(). You could be
dealing with a reserved word here.
 
M

m miller

Scott,

Good suggestion. I changed the function to hGlass() and I still get the same
behaviour. I also tested with:
document.form1.btnGet.style.cursor="wait";
in lieu of
document.body.style.cursor="wait";

and the same for the 'default' style, and got no errors. It seems to have a
problem with 'document.body.style'

Tks,
Marc Miller
 
M

MSFT

Hi Marc,

Is "btnFind" a ASP.NET Button control? When you click it, your setfous
method will be executed, however, the page also will be postback and
refresh. If you only want to setfocus to a TextBox, you may use a HTML
button component instead.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
V

Vidar Petursson

Hi

Well here is 1 error
if (document.body.style.cursor="wait") <-- error
if (document.body.style.cursor == "wait") <-- correct, 2 ==

document.body.style.cursor = (document.body.style.cursor =="wait") ? "auto"
: "wait";
document.forms["Form1"].txtPartno.focus();
document.forms["Form1"].txtPartno.select();

http://msdn.microsoft.com/library/d.../author/dhtml/reference/properties/cursor.asp

--
Best Regards
Vidar Petursson
==============================
Microsoft Scripting MVP
http://www.microsoft.com/technet/scriptcenter
==============================
 
S

Steven Cheng[MSFT]

Hi m miller,


Thank you for the response. Since you want to run some client script code
to set the cursor and some entryfields' focus, do you think it possible
that you used the HtmlControl button such as
<input type="button" id="btnFind" value="Find" onclick="setfocus" />

Since the ASP.NET server button will automatically posted back the page to
the serverside, so the "onclick" attribute you add for it won't be able to
run. To use a HtmlControl button, you can simple code it as this:
..............
<script language="javascript">
function setfocus()
{
if (document.body.style.cursor == "default")
{

document.body.style.cursor="wait";
document.Form1.txtPartno.focus();
document.Form1.txtPartno.select();
}
else
{
document.body.style.cursor="default";
document.Form1.txtPartno.focus();
document.Form1.txtPartno.select();
}
}
</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" align="center" border="1">
<tr>
<td><input type="text" id="txtOther" runat="server"></td>
<td><input type="button" id="btnFind" value="Find"
onclick="setfocus()"></td>
</tr>
<tr>
<td><INPUT id="txtPartno" type="text" name="txtPartno"
runat="server"></td>
<td></td>
</tr>
</table>
</form>
....................

Please try out the suggestion and see whether it helps. Also if you have
any questions on it or if my suggestion is not quite suitable for your
situation, please feel free to let me know.


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

m miller

Its nice to see the responses...Thanks!

Here's what I have tried.

1. I started with using an ASP.NET button control with :
Me.btnFind.Attributes.Add("onclick","hglass()") in the page_load.
2. I then added an HTML button as well and this is the code for that:

<INPUT id="btnFindHTML" style="Z-INDEX: 110; LEFT: 336px; POSITION:
absolute; TOP: 80px"
type="submit" value="Find" runat="server" onclick="hglass()">

They both call the code:
<script language="javascript">
function hglass(eventobj,obj)
{
if (document.body.style.cursor="default")
{

document.body.style.cursor="wait";
document.Form1.txtPartno.focus();
document.Form1.txtPartno.select();

}
else
{
document.body.style.cursor="default";
document.Form1.txtPartno.focus();
document.Form1.txtPartno.select();
}
}
</script>


The code works, but after one or two finds...IE blows up and crashes.

I tried using cursor=="default" in lieu of cursor="default" , but
the code does not execute.

Perhaps I will rebuid this app since the code works in another webapp that I
have.

Thanks for the help,
Marc Miller
 
S

Steven Cheng[MSFT]

Hi m miller,

Thanks for your reply. Please try out the following step:
1. remove the "runat=server" attribute in the
<INPUT id="btnFindHTML" style="Z-INDEX: 110; LEFT: 336px; POSITION:
absolute; TOP: 80px"
type="submit" value="Find" runat="server" onclick="hglass()">
Since the html button only call the client script, no need to be posted
back to server.

2. still using the cursor=="default" to compare the cusor's state

Try to see whether it works. Also, as you mentioned that your code works in
another project's certain page. Would you please have a check on that
page's code to see whether there are any differences between that one and
the page has error? I'm looking forward to your update.


Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

m miller

Steven,

The button needs to runat="server" because there is codebehind (VB) click
event where it goes out to Oracle and retrieves the data.
This is why I need an hourglass, because the query takes about 30 to 40
seconds to return the subset.

Marc
 
S

Steven Cheng[MSFT]

Hi m miller,

Thank you for the response. I think I've got your definite requirement, you
have to do some data retrieving operation which may take some pieces of
time when the ServerControl button is clicked. Since the operation won't be
finished in a short time, you want to set the client's cursor state to
"wait". I've done some further test on my side and you may try out the
following code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>CursorTest</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
<script language="javascript">
function ChangeCursor()
{
//alert(document.body.style.cursor)

if (document.body.style.cursor == "wait")
{
document.body.style.cursor="default";
document.Form1.txt2.focus();

}
else
{
document.body.style.cursor="wait";
document.Form1.txt2.focus();
}

}


</script>
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="500" border="1" align="center">
<tr>
<td><INPUT id="txt1" type="text" runat="server"></td>
<td></td>
</tr>
<tr>
<td><FONT face="ËÎÌå"><INPUT id="txt2" type="text"
runat="server"></FONT></td>
<td>
<asp:Button id="btnServer" runat="server" Text="Server
Button"></asp:Button></td>
</tr>
</table>
</form>
</body>
</HTML>

---------------------code behind---------------------
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here

btnServer.Attributes.Add("onclick","ChangeCursor()");
}


private void btnServer_Click(object sender, System.EventArgs e)
{
System.Threading.Thread.Sleep(1000*10);
Response.Write("<br>Server Button is clicked at:"+
DateTime.Now.ToLongTimeString());
}

Notice that the "document.body.style.cursor" is empty at first, so we can
check the cursor state like:
if (document.body.style.cursor == "wait")
{.......}else{.......}

And I use "System.Threading.Thread.Sleep(1000*10);" in the Serverside
button's click event to simulate the long time Data retrieving operation
you mentioned.

Hope it is helpful. If you have any question on it ,please feel free to
let me know.

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

MSFT

Hi Marc,

From your description, the problem is related to other components on the
same web form. A simple way to test this is adding an ASP.NET button on the
web form, in its code behind, adding following code:

System.Threading.Thread.Sleep(1000 * 10)

And then click it for 3~4 to see if it also will generate such error. If
so, you may check if there are sepcial components or code in the web form.
For example, are there some activex control, script block or ASP.NET
control on the web form. You may try to temporarily remove them from the
web form and test again to confirm.

Luke
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
M

m miller

I thank you for all your help. I did try the
System.Threading.Thread.Sleep(1000 * 10) from a test web form and it worked
well.
I then put it at the beginning of my btnFind_Click and a RETURN after it
and it crashed IE.

I then rebuilt the application in a new project, thru cut and paste. It now
works fine. The new project has all the elements of the
old project, help web forms, jpg's etc.

We may never know......

Marc Miller
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top