Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Button Redirect

Trying to get my button to act like a link (tried the <A> tag and it would work if you open in a new tab but not if you click on it. Then tried this code and nothing. Suggestions?

<button onClick="location.href='/secure/edit.aspx?id=671'">Edit</button>
like image 739
Geekender Avatar asked Sep 20 '12 21:09

Geekender


People also ask

Can a button redirect?

html button click redirectBy using HTML Anchor Tags <a>.. </a>, you can Redirect on a Single Button Click [html button click redirect]. To use HTML Anchor tags to redirect your User to Another page, you need to write your HTML Button between these HTML Anchor Tag's starting <a> and Closing </a> Tags.

How do I redirect a button to another page?

To make HTML submit button redirect to another page, we have to use HTML's Form tags or HTML Anchor Tags. HTML Form tags allow us to Show Form data and also allow us to submit that Form data to another page using HTML Form tag's Action Attribute. HTML Anchor can link any HTML Element to Another Page.

Can JavaScript redirect?

With a few lines of JavaScript code, you can redirect visitors to another URL. The recommended function is window. location. replace() .

How do I create a redirect Button in HTML?

Using button tag inside <a> tag: This method create a button inside anchor tag. The anchor tag redirect the web page into the given location. Adding styles as button to a link: This method create a simple anchor tag link and then apply some CSS property to makes it like a button.


2 Answers

You need to explicitly say window.location because this in the context of the button is the button object itself. Ordinarily, JavaScript is run in the context of the window object, so you don't need to do that.

<button onClick="javascript:window.location.href='/secure/edit.aspx?id=671'">Edit</button>

(Additionally, I also like to explicitly state that the script is javascript:, but that's purely a personal thing.)

like image 104
Oliver Moran Avatar answered Sep 20 '22 06:09

Oliver Moran


<button onclick="window.location='/secure/edit.aspx?id=671'">Edit</button>
like image 42
AlienWebguy Avatar answered Sep 23 '22 06:09

AlienWebguy