Ales Ales - 5 months ago 16
HTML Question

AJAX get backup when no javascript

What is the best method to handle a AJAX get if there is no javascript enabled on browser?

For an example, a simple click on image thumbnail directs you to a controller that returns image details and zoomed photo.

I wanted to do this with ajax and show the bigger picture with details on the same page.

But I don't know how to implement backup method when user doesn't have javascript.

Can i delegate click method on anchor container and then somehow stop bubbling? And then when there is no javascript does the anchor gets triggered? The details won't be on same page, but atleast they will show.

Answer

Can i delegate click method on anchor container and then somehow stop bubbling?

Basically, yes. Have the link around the image (or whatever):

<a class="image-link" href="link/to/the/details">...</a>

And handle it in JavaScript:

$(document.body).on("click", ".image-link", function(e) {
    // We're handling it with ajax, don't do the default action
    // (which is following the link)
    e.preventDefault();
    // ...do the ajax stuff...
});

If JavaScript isn't enabled, the handler isn't hooked up, and the link gets followed normally. If JavaScript is enabled, we prevent the default action, which prevents the link being followed, and do the ajax instead.

Comments