开发者

Why won't my javascript buttons redirect like they're supposed to?

I'm sorry to ask such a simple question with such a small amount of info, but I just can't figure this out!

I have an ASP.NET page, into which I've added several plain-vanilla HTML buttons that are supposed to take the user to different pages. Here's one example:

<button onclick="location.href='Default.aspx';">Go Back</button>

I would expect that when clicked, this button would redirect the browser to Default.aspx. Instead, it just seems to refresh the current page, keeping the URL intact.

The buttons are inside the page-wide ASP.NET <form> tag. The buttons do not submit the form. When I view the source code for the page in the browser, I do not see anything that should intercept a change in location (like an onunload event handler or anything like that). There are no other frames. There's really nothing remarkable about this page at all.

I've tried using location=, self.location=, window.location=, and location.replace(). None of them work when executed from within the button's onclick handler. I have tried in Firefox 5 and Chrome 10. The javascript does work when executed from the browser's address bar like so: javascript: location.href='Default.aspx'; void(0);.

I've used Firebug's javascript debugger to try to trace what happens when I click the button, and I haven't seen anything unusual. I don't see any event-intercepting code or other issues. There are no error logged in the error console.

It seems to me that I've used script like the before and it's always worked just fine. Why isn't it working now? Some code created by ASP.NET that I don't know about? Is onclick not functional from within a <button>? Is this some JS security oddity that both Chrome and Firefox开发者_运维问答 share?

Please help, this is driving me crazy!! Thanks.


Just add return false; like this:

<button onclick="location.href='Default.aspx';return false;">Go Back</button>

to stop the button submitting the form to itself.


Try adding return false to the onclick javascript?


You need to use document.location.href="your url";

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜