window.open using onmouseover vs onclick

N

Nathan Sokalski

I want to trigger a download using the onmouseover event. If I open the
browser window using the onmouseover or onclick event and window.open
method, it shows the Open/Save/Cancel dialog like I would expect and want.
However, when I click Open or Save when it was opened using onclick, it asks
you where to save the file and downloads as expected, but if it was opened
using onmouseover clicking Open or Save just closes the dialog, and does not
do the download. Here is a page that I used to test this:


<%@ Page Language="vb" AutoEventWireup="false"
CodeBehind="downloadtester.aspx.vb" Inherits="FileManageTest.downloadtester"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Download Tester</title>
</head>
<body id="BodyTag" runat="server">
<form id="form1" runat="server">
<asp:Label ID="lblClick" runat="server" Text="click"
Font-Bold="true" Font-Size="Large"/><br/>
<div id="div1"
style="background-color:Teal;width:300px;height:300px;"></div><br/><br/><br/>
<asp:Label ID="lblMouseOver" runat="server" Text="mouseover"
Font-Bold="true" Font-Size="Large"/><br/>
<div id="div2"
style="background-color:Green;width:300px;height:300px;"></div>
</form>
</body>
</html>

Partial Public Class downloadtester : Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Me.BodyTag.Attributes.Add("onclick",
String.Format("if(event.type=='click'&&event.srcElement.id=='div1'){{window.open('downloadfile.aspx?filepath={0}&extension={1}','newwindow');}}",
Server.UrlEncode("C:|config.sys"), Server.UrlEncode(".sys")))
Me.BodyTag.Attributes.Add("onmouseover",
String.Format("if(event.type=='mouseover'&&event.srcElement.id=='div2'){{window.open('downloadfile.aspx?filepath={0}&extension={1}','newwindow');}}",
Server.UrlEncode("C:|config.sys"), Server.UrlEncode(".sys")))
End Sub
End Class


As you can see, the window.open() statements for both the onclick and
onmouseover events are identical, and even though they both ask you if you
want to do the download, only the onclick actually allows you to do it. Why
is this, and what can I do to fix it? The browser that these experiments
were done with is IE7 on Windows Vista. Thank you for any help you can give.
 
B

bruce barker

i'd guess its a bug with the IE builtin popup blocker. the save dialog
should not appear at all on mouseover (the open should be ignored).

-- bruce (sqlwork.com)
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Nathan said:
I want to trigger a download using the onmouseover event. If I open the
browser window using the onmouseover or onclick event and window.open
method, it shows the Open/Save/Cancel dialog like I would expect and
want. However, when I click Open or Save when it was opened using
onclick, it asks you where to save the file and downloads as expected,
but if it was opened using onmouseover clicking Open or Save just closes
the dialog, and does not do the download. Here is a page that I used to
test this:


<%@ Page Language="vb" AutoEventWireup="false"
CodeBehind="downloadtester.aspx.vb"
Inherits="FileManageTest.downloadtester" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Download Tester</title>
</head>
<body id="BodyTag" runat="server">
<form id="form1" runat="server">
<asp:Label ID="lblClick" runat="server" Text="click"
Font-Bold="true" Font-Size="Large"/><br/>
<div id="div1"
style="background-color:Teal;width:300px;height:300px;"></div><br/><br/><br/>

<asp:Label ID="lblMouseOver" runat="server" Text="mouseover"
Font-Bold="true" Font-Size="Large"/><br/>
<div id="div2"
style="background-color:Green;width:300px;height:300px;"></div>
</form>
</body>
</html>

Partial Public Class downloadtester : Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Me.BodyTag.Attributes.Add("onclick",
String.Format("if(event.type=='click'&&event.srcElement.id=='div1'){{window.open('downloadfile.aspx?filepath={0}&extension={1}','newwindow');}}",
Server.UrlEncode("C:|config.sys"), Server.UrlEncode(".sys")))
Me.BodyTag.Attributes.Add("onmouseover",
String.Format("if(event.type=='mouseover'&&event.srcElement.id=='div2'){{window.open('downloadfile.aspx?filepath={0}&extension={1}','newwindow');}}",
Server.UrlEncode("C:|config.sys"), Server.UrlEncode(".sys")))
End Sub
End Class


As you can see, the window.open() statements for both the onclick and
onmouseover events are identical, and even though they both ask you if
you want to do the download, only the onclick actually allows you to do
it. Why is this, and what can I do to fix it? The browser that these
experiments were done with is IE7 on Windows Vista. Thank you for any
help you can give.

I see that you are opening both URLs into the same window. Try using
'_blank' instead of 'newwindow', that will cause them to always open in
a new window.
 
N

Nathan Sokalski

You have the JavaScript window.open confused with the HTML <a> tag's target
attribute. The parameter that I use the value 'newwindow' for is just a
name; it has nothing to do with whether the window opens in a new window.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Nathan said:
You have the JavaScript window.open confused with the HTML <a> tag's
target attribute.

Not at all.
The parameter that I use the value 'newwindow' for is
just a name; it has nothing to do with whether the window opens in a new
window.

It most definitely does. If there is a window with that name already,
the page will open in that window, not in a new window. If you specify
'_blank', it will always open in a new window.

RTFM:
http://msdn2.microsoft.com/en-us/library/ms536651.aspx
 
N

Nathan Sokalski

It's not the onmouseover that's triggering the Download Dialog, it's the
page being opened by the window.open() method. The page being opened by the
window.open() method starts a download, which is what it is supposed to do.
 
N

Nathan Sokalski

OK, my mistake, but that is not the issue here, because there are no other
windows open when I call window.open, and I programmatically close the
window afterwards anyway.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Nathan said:
OK, my mistake, but that is not the issue here, because there are no other
windows open when I call window.open, and I programmatically close the
window afterwards anyway.

It may be the issue if you first open the window by clicking, then the
mouseover method might not be able to open a new window while the first
is downloading.

I tried out your code. It works just fine in IE6, but not in IE7. I
suspect that this is something that is added for the protection of the
user, so that a download doesn't occur if the user didn't activate any
control. It works just fine if the opened page is not a download.

I changed the code so that it works in other browsers too, and it works
just fine in Firefox 2, Opera 9 and Netscape 8 (after disabling some
popup blocking in each).
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top