How to make an input type=button act like a hyperlink and redirect using a GET request?
Redirect upon button click using:
This code piece will direct your browser to https://example.com
on a button click with a good old GET request.
When to use Javascript for redirection
The Fast answer is using Javascript to redirect the user, ideal when:
- You need to mimic button functionality but want to avoid the regular form submission.
- A simple, no-nonsense GET request to a specific URL is called upon.
- Dealing with Javascript libraries or complex frameworks is so not your groove.
Alternatives for button-like hyperlinks
If the onclick Javascript flavor isn't to your taste, the HTML menu offers a few goodies:
-
The Disguised Anchor: The humble
<a>
tag can dress up as a button, thanks to a bit of CSS magic. Hyperlink capabilities of<a>
intact. -
The Form Route: A traditional way using an
<input type="submit">
inside a<form>
tag. Just remember to set the form method to"get"
.
Including parameters in GET requests
There's a twist in the plot - you can encode parameters in the URL when you're feeling extra loggerheaded:
This passes a search term as a parameter along with a dance to the server.
Handling complex redirections with onclick
The onclick event in Javascript can swiftly turn into a potent tool for advancing navigation:
-
Conditional Redirection: Yes, the button can think before it leaps. Based on user input or other dynamic factors, if-else can be a wise option.
-
Appending Query Parameter: That little button can carry data all the way from an input field to the URL. Talk about sneaky!
-
Champion Navigator: Want to go back upon faltering at the form validity check? This bot's got your back!
SEO and UX: Bridging the gap
When it's not all about making buttons dance, consider SEO and UX principles:
- Primary Navigation: Stick to
<a>
tags if you're targeting search engine robots, not Terminators🤖. - Inside Actions: For in-app actions,
<button>
or<input>
can do the hustle. - Clarity in Action: A button should scream out its purpose - remember, your users are not psychic (unless they are, in which case, hello there!).
Was this article helpful?