最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

open a window (aspx page) with one click + javascript - Stack Overflow

programmeradmin2浏览0评论

I need to open a new window with a single click of an asp button. My problem is that it always takes two clicks.. if i write the open window code in the page load, then the window gets open on 1 click.. any ideas how to get around this...

Button Click Code :

btnClaim.Attributes.Add("Onclick","javascript:return OpenPopup()")

Javascript function :

  function OpenPopup()
{
window.open("newWindow.aspx?", "_blank", "height=500, width=575, left=150,
top=150, " +
"location=no, menubar=no, resizable=no, " +
"scrollbars=no, titlebar=no, toolbar=no", true);
}  

I need to open a new window with a single click of an asp button. My problem is that it always takes two clicks.. if i write the open window code in the page load, then the window gets open on 1 click.. any ideas how to get around this...

Button Click Code :

btnClaim.Attributes.Add("Onclick","javascript:return OpenPopup()")

Javascript function :

  function OpenPopup()
{
window.open("newWindow.aspx?", "_blank", "height=500, width=575, left=150,
top=150, " +
"location=no, menubar=no, resizable=no, " +
"scrollbars=no, titlebar=no, toolbar=no", true);
}  
Share Improve this question asked Oct 22, 2010 at 17:25 BumbleBeeBumbleBee 10.8k20 gold badges80 silver badges124 bronze badges 3
  • Off-topic because I don't think it has anything to do with your actual problem, but: onclick is not a hyperlink attribute, you don't need (or want) the javascript: prefix. It shouldn't be doing any harm (because it looks like a JavaScript label, so isn't a syntax error), but it's useless at best. (If you were using href, it would be a different story.) – T.J. Crowder Commented Oct 22, 2010 at 17:28
  • Also off-topic: You want to get rid of the spaces after the mas in your window.open call. (e.g., ...width=575,left=150... not ...width=575, left=150... The original window.open actually explicitly disallowed them (and tended to balk on them), and I wouldn't bet that some UAs don't still do that. – T.J. Crowder Commented Oct 22, 2010 at 17:30
  • (In case you're wondering, no, I have no idea why it would take two clicks. It shouldn't, and it wouldn't if you were doing pure HTML+JavaScript.) – T.J. Crowder Commented Oct 22, 2010 at 17:31
Add a ment  | 

2 Answers 2

Reset to default 4

The problem is that you are adding code to handle a click in the click event handler. That means that when you click the button the first time the event handler adds the attribute to the button. After the postback you can click the button again to open the popup, as the button now has the client side code.

Either add the attribute in Page_Load so that the button always has the attribute, or in the event handler add code to the page that calls the function immediately after postback:

ClientScript.RegisterStartupScript(Me.GetType(), "open", "OpenPopup();", True)

This problem likely occurs because the form in being submitted on click (PostBack). If you return false in the onclick attribute, it should cancel a form submit. You can also use OnClientClick.

btnClaim.OnClientClick = "javascript:OpenPopup(); return false;";
发布评论

评论列表(0)

  1. 暂无评论