window.open using onmouseover vs onclick

Discussion in 'ASP .Net' started by Nathan Sokalski, Oct 12, 2007.

  1. 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.
    Nathan Sokalski, Oct 12, 2007
    #1
    1. Advertising

  2. Nathan Sokalski

    bruce barker Guest

    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)

    Nathan Sokalski wrote:
    > 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.
    bruce barker, Oct 12, 2007
    #2
    1. Advertising

  3. Nathan Sokalski wrote:
    > 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.

    --
    Göran Andersson
    _____
    http://www.guffa.com
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Oct 12, 2007
    #3
  4. 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.

    "Göran Andersson" <> wrote in message
    news:%...
    > Nathan Sokalski wrote:
    >> 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.
    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
    Nathan Sokalski, Oct 12, 2007
    #4
  5. Nathan Sokalski wrote:
    > 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

    --
    Göran Andersson
    _____
    http://www.guffa.com
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Oct 12, 2007
    #5
  6. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "bruce barker" <> wrote in message
    news:%...
    > 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)
    >
    > Nathan Sokalski wrote:
    >> 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.
    Nathan Sokalski, Oct 13, 2007
    #6
  7. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Göran Andersson" <> wrote in message
    news:...
    > Nathan Sokalski wrote:
    >> 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
    >
    > --
    > Göran Andersson
    > _____
    > http://www.guffa.com
    Nathan Sokalski, Oct 13, 2007
    #7
  8. Nathan Sokalski wrote:
    > 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).

    --
    Göran Andersson
    _____
    http://www.guffa.com
    =?ISO-8859-1?Q?G=F6ran_Andersson?=, Oct 13, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PC HUA
    Replies:
    2
    Views:
    393
    PC HUA
    May 19, 2004
  2. Replies:
    3
    Views:
    111
  3. Replies:
    6
    Views:
    164
  4. Replies:
    2
    Views:
    98
  5. JJA
    Replies:
    0
    Views:
    150
Loading...

Share This Page