开发者

Javascript opens new window despite no call

I have a javascript that opens a new window for "PreChecking" a site. The problem is when i click the button, it works fine... but w开发者_开发技巧ichever i button i press next makes the method fire again, despite i doesn't call it. Its just the button with id "lnkPrecheck" that should call the method. Have searched far and wide for a slolution that just opens a new window for the lnkPrecheck button, not the others on the site. THere must be a way for only 1 of 3 buttons makes the function call, not all of them!

The code:

<asp:Button OnClick="lnkPrecheck_Click" OnClientClick="NewWindow();" ID="lnkPrecheck" runat="server" text="Precheck (Opens in a new window)" />

    function NewWindow() {
            document.forms[0].target = "_blank";
}


Try this:

<script type="text/javascript">    
    var NewWindow = function() {
        document.forms[0].target = "_blank";
        return false; // Prevents ASP.NET-buttons from performing default behavior
    }    
</script>

<asp:Button OnClick="lnkPrecheck_Click" OnClientClick="NewWindow();" ID="lnkPrecheck" runat="server" text="Precheck (Opens in a new window)" />

Not sure if setting the target of a form to _blank will open a new window though.


If you have any <input type="button" ... /> or <button> tags within a form, you have to make their onclick handlers return false (or otherwise cancel any click events). Otherwise their default behavior is to submit the form, even though they're not explict "submit" buttons.


There is no real "solution" to your problem.

Once you set the form target, it stick for good. It's not reverting back to the original target.

One way around is store the original target as custom attribute then "reset" the target when clicking other buttons.

Code for that would be:

function NewWindow() {
   var oForm = document.forms[0];
   var curTarget = oForm.target;
   if (curTarget != "_blank")
      oForm.setAttribute("org_target", curTarget);
   oForm.target = "_blank";
}

function ResetTarget() {
   var oForm = document.forms[0];
   oForm.target = oForm.getAttribute("org_target");
}

Then in all other buttons call the ResetTarget function first.


Once try this...
It's working for me..
Use this for the asp.net control where you want to open in a new tab.

 OnClientClick="window.document.forms[0].target='_new'; setTimeout(function(){window.document.forms[0].target='';}, 500);"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜