How can i create a button that opens another page in html? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i create a button that opens another page in html?

HTML

27th Jun 2020, 12:34 PM
Bikila Ketema
Bikila Ketema - avatar
9 Answers
+ 3
Wrapping the button inside a <a> tag seems to work <a href="https://google.com/"> <button type="button">Google</button> </a> (Edit) Upon a note from my friend EmmanueLZ, I had just found out that such approach is invalid. So I'm proposing another approach by the use of `window.open` method. https://www.w3schools.com/jsref/met_win_open.asp <button type="button" onclick="window.open('https://www.google.com/', '_blank');">Google </button>
27th Jun 2020, 12:53 PM
Ipang
+ 2
Use javascript for this purpose. There are plenty of things which can be done using javascript. Else simply use anchor tag and make it to look like a button using css
27th Jun 2020, 12:44 PM
Ayush Kumar
Ayush Kumar - avatar
+ 2
In writing and markup, a functionality which when clicked will open a web page is called 'hyperlink'. Hyperlinks in HTML are created with anchor <a> tags. It is not semantically correct to use a <button> for this. Bikila Ketema Can you give more details on why you want to use a button? In my opinion, you should use <a> tags. You can style them like a button with css but you should not except it performs as a button e.g an "open cart" link. If that is the case make sure to give it a role="button" attribute.
27th Jun 2020, 9:43 PM
Ore
Ore - avatar
+ 2
Now listen, there are many ways to do this both in js, php and html. Based on your question, you can simply use anchor tags <a href="http://">Button</a> But in js, you can use window.location property
27th Jun 2020, 11:46 PM
Clinton Ufere
Clinton Ufere - avatar
+ 1
Ipang ,Bikila Ketema hi Indeed despite the fact that a button nested in an <a> tag work, when go and check your code in the w3c validator, it raises an error. -->button is not allowed to be used as a descendant of an <a> tag.... I don't know why, maybe because of semantics accessibility and search engines, they don't explain. Depending what your working on, though, it have to be considered. But it do work....
27th Jun 2020, 2:24 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 1
EmmanueLZ. Thank you for the heads up 🙏 I didn't validate it just saw it worked 😁 Will note that ...
27th Jun 2020, 2:27 PM
Ipang
+ 1
Ipang , you're welcome, I did the same, coding a bunch of that on a page, thought it was ok but then the validation and had to change everything.🤪
27th Jun 2020, 2:32 PM
EmmanueLZ.
EmmanueLZ. - avatar
+ 1
I have got the Answer, Thanks!
29th Jun 2020, 6:06 AM
Bikila Ketema
Bikila Ketema - avatar
- 1
simple:: <a href="another.html"><button>click</button></a>
28th Jun 2020, 10:04 PM
marc
marc - avatar