Javascript弹出窗口在某些情况下失去焦点

时间:2016-04-05 19:24:34

标签: javascript c# asp.net

这绝对令我感到困惑。以下代码有效:

<a href="javascript:void(0);" onclick="openPopUp('Edit_Screen.aspx?Image_Id='+ '<%# Eval("Image_Id") %>','Popup',850,800);">
                         <%# Eval("ReferenceNumber") %>
</a>

这称之为:

function openPopUp(pageURL, title, popupWidth, popupHeight) {
  var targetPop = null;
  var left = (screen.width / 2) - (popupWidth / 2);
  var top = (screen.height / 2) - (popupHeight / 2);
  targetPop = window.open(pageURL, title, 'toolbar=no, status=no, menubar=no, width=' + popupWidth + ', height=' + popupHeight + ', top=' + top + ', left=' + left).focus();

}

此代码不起作用:

<asp:Button ID="btnNewRecord" runat="server" Text="Enter New Record" CssClass = "button" OnClientClick="LaunchImage()" />

这称之为:

function LaunchImage()
{
   debugger;
   openPopUp("Insert_Screen.aspx?dataType=images", "Popup", 750, 650);
}

并调用上面显示的相同openPopUp函数。第一个href代码示例调用弹出窗口并将焦点保持在弹出窗口上。第二个,从ASP按钮调用,调用弹出窗口,然后将焦点返回到调用它的屏幕。有谁知道为什么?这一直困扰着我一段时间。

提前致谢!

2 个答案:

答案 0 :(得分:0)

单击该按钮会导致回发,而单击锚定链接则不会。您可以通过在按钮的OnClientClick事件中返回false来阻止回发:

OnClientClick="LaunchImage(); return false;"

答案 1 :(得分:0)

<asp:Button ID="btnNewRecord" runat="server" Text="Enter New Record" CssClass = "button" OnClientClick="LaunchImage(); return false;" />

<asp:Button ID="btnNewRecord" runat="server" Text="Enter New Record" CssClass = "button" OnClientClick="return LaunchImage();" />

和javascript

function LaunchImage()
{
   debugger;
   openPopUp("Insert_Screen.aspx?dataType=images", "Popup", 750, 650);
   return false;
}