Use onclick event when JS is enabled, go to url when it is not

Facebook uses Ajax links to change parts of its pages to reduce load time. But if you disable JavaScript, their links still work. This is because they also identified a backup option: classic <a href="http://url/".

How can I implement this on my own so that the connection is executed when JS is disabled and the onclick event will be used when JS is turned on?

+3
source share
2 answers

As onclickyou do return false;at the end to prevent the default action, which in this case actually moves in the URL-address, for example:

document.getElementById('linkId').onclick = function() {
  alert("This link would go to: " + this.href);
  return false;
};

, - jQuery event.preventDefault():

$("a").click(function(e) {
  alert("This link would go to: " + this.href);
  return false; //or e.preventDefault();
});
+6

onclick false, , js . href .

+1

Source: https://habr.com/ru/post/1745775/


All Articles