How to create an HTML button that acts like a link?

后端 未结 30 3683
长发绾君心
长发绾君心 2020-11-21 04:18

I would like to create an HTML button that acts like a link. So, when you click the button, it redirects to a page. I would like it to be as accessible as possible.

相关标签:
30条回答
  • 2020-11-21 05:02

    Why not just place your button inside of a reference tag e.g

    <a href="https://www.google.com/"><button>Next</button></a>
    

    This seems to work perfectly for me and does not add any %20 tags to the link, just how you want it. I have used a link to google to demonstrate.

    You could of course wrap this in a form tag but it is not necessary.

    When linking another local file just put it in the same folder and add the file name as the reference. Or specify the location of the file if in is not in the same folder.

    <a href="myOtherFile"><button>Next</button></a>
    

    This does not add any character onto the end of the URL either, however it does have the files project path as the url before ending with the name of the file. e.g

    If my project structure was...

    .. denotes a folder - denotes a file while four | denote a sub directory or file in parent folder

    ..public
    |||| ..html
    |||| |||| -main.html
    |||| |||| -secondary.html

    If I open main.html the URL would be,

    http://localhost:0000/public/html/main.html?_ijt=i7ms4v9oa7blahblahblah
    

    However, when I clicked the button inside main.html to change to secondary.html, the URL would be,

    http://localhost:0000/public/html/secondary.html 
    

    No special characters included at the end of the URL. I hope this helps. By the way - (%20 denotes a space in a URL its encoded and inserted in the place of them.)

    Note: The localhost:0000 will obviously not be 0000 you'll have your own port number there.

    Furthermore the ?_ijt=xxxxxxxxxxxxxx at the end off the main.html URL, x is determined by your own connection so obviously will not be equal to mine.


    It might seem like I'm stating some really basic points but I just want to explain as best as I can. Thank you for reading and I hope this help someone at the very least. Happy programming.

    0 讨论(0)
  • 2020-11-21 05:02

    If what you need is that it will look like a button, with emphasis on the gradient image, you can do this:

    <a href="www.yourlink.com" class="btn btn-gradient"><i class="fa fa-home"> Button Text</i></a>
    
    0 讨论(0)
  • 2020-11-21 05:03
    <form>
        <input type="button" value="Home Page" onclick="window.location.href='http://www.wherever.com'"> 
    </form>
    
    0 讨论(0)
  • 2020-11-21 05:03

    There seems to be three solutions to this problem (all with pros and cons).

    Solution 1: Button in a form.

    <form method="get" action="/page2">
        <button type="submit">Continue</button>
    </form>
    

    But the problem with this is that in some version of popular browsers such as Chrome, Safari and Internet Explorer, it adds a question mark character to the end of the URL. So in other words for the code above your URL will end up looking like this:

    http://someserver/pages2?
    

    There is one way to fix this, but it will require server-side configuration. One example using Apache Mod_rewrite would be to redirect all requests with a trailing ? to their corresponding URL without the ?. Here is an example using .htaccess, but there is a full thread here:

    RewriteCond %{THE_REQUEST} \?\ HTTP [NC]
    RewriteRule ^/?(index\.cfm)? /? [R=301,L]
    

    Similar configurations can vary depending on the webserver and stack used. So a summary of this approach:

    Pros:

    1. This is a real button, and semantically it makes sense.
    2. Since it is a real button, it will also act like a real button (e.g. draggable behavior and/or mimic a click when pressing space bar when active).
    3. No JavaScript, no complex style required.

    Cons:

    1. Trailing ? looks ugly in some browsers. This can be fixed by a hack (in some cases) using POST instead of GET, but the clean way is to have a server-side redirect. The downside with the server side redirect is that it will cause an extra HTTP call for these links because of the 304 redirect.
    2. Adds extra <form> element
    3. Element positioning when using multiple forms can be tricky and becomes even worse when dealing with responsive designs. Some layout can become impossible to achieve with this solution depending on the order of the elements. This can end up impacting usability if the design is impacted by this challenge.

    Solution 2: Using JavaScript.

    You can use JavaScript to trigger onclick and other events to mimic the behavior of a link using a button. The example below could be improve and remove from the HTML, but it is there simply to illustrate the idea:

    <button onclick="window.location.href='/page2'">Continue</button>
    

    Pros:

    1. Simple (for basic requirement) and keep semantic while not requiring an extra form.
    2. Since it is a real button, it will also act like a real button (e.g. draggable behavior and/or mimic a click when pressing space bar when active).

    Cons:

    1. Requires JavaScript which means less accessible. This is not ideal for a base (core) element such as a link.

    Solution 3: Anchor (link) styled like a button.

    Styling a link like a button is relatively easy and can provide similar experience across different browsers. Bootstrap does this, but it is also easy to achieve on your own using simple styles.

    Pros:

    1. Simple (for basic requirement) and good cross-browser support.
    2. Does not need a <form> to work.
    3. Does not need JavaScript to work.

    Cons:

    1. Semantic is sort of broken, because you want a button that acts like a link and not a link that acts like a button.
    2. It will not reproduce all behaviors of solution #1. It will not support the same behavior as button. For example, links react differently when dragged. Also the "space bar" link trigger will not work without some extra JavaScript code. It will add a lot of complexity since browsers are not consistent on how they support keypress events on buttons.

    Conclusion

    Solution #1 (Button in a form) seems like the most transparent for users with minimal work required. If your layout is not impacted by this choice and the server side tweak is feasible, this is a good option for cases where accessibility is the top priority (e.g. links on an error page or error messages).

    If JavaScript is not an obstacle to your accessibility requirements, then solution #2 (JavaScript) would be preferred over #1 and #3.

    If for some reason, accessibility is vital (JavaScript is not an option) but you are in a situation where your design and/or your server configuration is preventing you from using option #1, then solution #3 (Anchor styled like a button) is a good alternative solve this problem with minimal usability impact.

    0 讨论(0)
  • 2020-11-21 05:04

    I used this for a website I'm currently working on and it works great!. If you want some cool styling too I'll put the CSS down here.

    input[type="submit"] {
      background-color: white;
      width: 200px;
      border: 3px solid #c9c9c9;
      font-size: 24pt;
      margin: 5px;
      color: #969696;
    }
    
    input[type="submit"]:hover {
      color: white;
      background-color: #969696;
      transition: color 0.2s 0.05s ease;
      transition: background-color 0.2s 0.05s ease;
      cursor: pointer;
    }
    <input type="submit" name="submit" onClick="window.location= 'http://example.com'">

    Working JSFiddle here.

    0 讨论(0)
  • 2020-11-21 05:06

    It is actualy very simple and without using any form elements. You can just use the <a> tag with a button inside :).

    Like this:

    <a href="http://www.google.com" target="_parent"><button>Click me !</button></a>
    

    And it will load the href into the same page. Want a new page? Just use target="_blank".

    EDIT

    Couple of years later, while my solution still works, keep in mind you can use a lot of CSS to make it look whatever you want. This was just a fast way.

    0 讨论(0)
提交回复
热议问题