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

jquery - How to stop a page from loading with JavaScript - Stack Overflow

programmeradmin2浏览0评论

I have a Button with PostBackUrl. Also I have assigned a function to its onclientclick method in which some page logic were written including a couple of validation rules. if whole page were not valid this method returns False but I can not stop page. I have tried document.execCommand('Close') and Window.Stop() but no success.

Any suggestions?

Edit: Button:

<asp:Button ID="btnSubmit" runat="server" OnClientClick="return DoLogic()"  PostBackUrl="" Class="btn_submit"/>

DoLogic:

function DoLogic() {

if (ValidateForm()) {
blah blah blah
return true;
}
else {
alert("Has Error");
return false;
}

ValidateForm:

function ValidateForm(){
if (haserror)  
{  
return false; 
} 
return true; 
}

I have a Button with PostBackUrl. Also I have assigned a function to its onclientclick method in which some page logic were written including a couple of validation rules. if whole page were not valid this method returns False but I can not stop page. I have tried document.execCommand('Close') and Window.Stop() but no success.

Any suggestions?

Edit: Button:

<asp:Button ID="btnSubmit" runat="server" OnClientClick="return DoLogic()"  PostBackUrl="http://google." Class="btn_submit"/>

DoLogic:

function DoLogic() {

if (ValidateForm()) {
blah blah blah
return true;
}
else {
alert("Has Error");
return false;
}

ValidateForm:

function ValidateForm(){
if (haserror)  
{  
return false; 
} 
return true; 
}
Share Improve this question edited Jul 24, 2013 at 21:46 amir moradifard asked Jul 24, 2013 at 21:13 amir moradifardamir moradifard 3531 gold badge9 silver badges26 bronze badges 5
  • Try to set onclientclick="return DoLogic()" like this. – maketest Commented Jul 24, 2013 at 21:16
  • window.stop() is lowercase, which does matter. i'm not sure if stop() still works like it used to. a good throw "stop!"; should also do the trick for the current script block. – dandavis Commented Jul 24, 2013 at 21:23
  • @maketest, I changed it to <asp:Button ID="btnSubmit" runat="server" onclientclick="return DoLogic()" PostBackUrl="google." Class="btn_submit"/> However it stops the page from redirecting, but it stops the page even if the Validation returns True value! – amir moradifard Commented Jul 24, 2013 at 21:26
  • See my answer for some insight on how to use PostBackUrl – Ross Brasseaux Commented Jul 24, 2013 at 21:47
  • 1 @amirmoradifard try updating your onclientclick attribute to "if (!DoLogic()) return false;" instead of "return DoLogic()" – Dr. Wily's Apprentice Commented Jul 24, 2013 at 22:29
Add a ment  | 

2 Answers 2

Reset to default 3

You can use .preventDefault() method on your event. This will stop the default action of a page load when the button is clicked.

http://www.w3/TR/DOM-Level-2-Events/events.html#Events-flow-cancelation

You can also return false; inside your DoLogic(). This will stop the page load as well.

onclientclick="return DoLogic()"

Then in your function:

function DoLogic(){
    //blah blah blah
    return false;
}

UPDATE

If you're doing all of this and are still having problems, try adding this to your button:

onclick="javascript:return false"

UPDATE FROM OP

Your code isn't working because in the if statement you return true; and false only gets returned if the shit hits the fan (ie an error). You need to return false everytime. OR use the .preventDefault() like I mentioned earlier.

I've read that the PostBackUrl can give you trouble, and especially in this case because it looks like you're using it wrong. I'm thinking you may be confused about what PostBackUrl does. It is an attribute used in Cross-Page posting. If you are trying to redirect the user to a different page when the postback is pleted, use Response.Redirect() in the code-behind or use an <asp:HyperLink> tag instead.

There should be no reason for which you could not use Response.Redirect(). Just add it to the end of your script in the code-behind. If the client-side validation returns false, the life-cycle will not reach the server-side anyway.

All-in-all it looks like you have at least three errors in that control:

  1. You definitely need the return part of the OnClientClick attribute.
  2. You need to change the Class attribute to CssClass.
  3. You probably need to use something other than PostBackUrl on that button.

You can read more about it here:

  • MSDN - Button.PostBackUrl

To elaborate . . .

If you are trying to send a user to another site when the code is finished executing, the PostBackUrl attribute cannot be used in the manner you have shown us. For instance, in your example, even if it did work, the user would likely see this:

发布评论

评论列表(0)

  1. 暂无评论