Search code examples
htmlhref

Is it possible to put several URL in a href parameter?


I want to put a link with the <a> tag on my website page, but with multiple URLs in case the 1st one doesn't work. I would be something like this:

<a href="url1;url2;url3"

But of course this doesn't work. So is there a way to do this ?

Thank you


Solution

  • The closest you could come would be to:

    1. Put a default URL in the href
    2. List alternative URLs elsewhere (such as in a data attribute)
    3. Bind a click event handler that runs some JavaScript which prevents the default behaviour and then tests each URL in turn until it finds one that works (or runs out).

    Testing would require Ajax which would require either:

    • Permission via CORS from each URL you test
    • The user of a proxy (which would test availability of the URL to your proxy and not to the browser)

    The additional HTTP requests would consume bandwidth (and thus time).