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

asp.net - Why won't my javascript buttons redirect like they're supposed to? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Jun 29, 2011 at 18:20 Joshua CarmodyJoshua Carmody 13.7k16 gold badges67 silver badges84 bronze badges 5
  • 2 does it have to be a button? seems like this is what the <a> tags are for. – Al W Commented Jun 29, 2011 at 18:23
  • this fiddle seems to work in Chrome 12 and FF5: jsfiddle/uD3qS – Mrchief Commented Jun 29, 2011 at 18:27
  • @AI W - Yeah, they were originally <a> tags, but I was asked to change them to buttons because they didn't stand out enough. I could use <a> tags and style them as buttons or use images, but I thought this way would be easy.... – Joshua Carmody Commented Jun 29, 2011 at 18:29
  • It works fine for me. Sorry :\ Try using a link button for the same effect – Nick Rolando Commented Jun 29, 2011 at 18:30
  • @Mrchief - Yeah, go figure. So it must be something specific to the page, right? – Joshua Carmody Commented Jun 29, 2011 at 18:33
Add a ment  | 

3 Answers 3

Reset to default 10

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)

  1. 暂无评论