user3916382
user3916382

Reputation:

Alternative Anchor Tag For When Javascript Is Not Enabled

It is possible to have an anchor tag have a different "href" attribute when javascript is not enabled.

For Example:

Javascript Enabled:

<a href="#news">News</a>

Javascript Not Enabled:

<a href="news.html">News</a>

Upvotes: 0

Views: 79

Answers (1)

user228534
user228534

Reputation:

You can add the javascript enabled attribute in a data-xxxx, and then use javascript to replace the href with data-xxxx.

For example, in HTML you could use

<a href="news.html" data-js-href="#news">News</a>

And in javascript, you could do

var links = document.querySelectorAll('a[data-js-href]');
for (var i=0; i<links.length; ++i) {
    links[i].setAttribute('href', links[i].getAttribute('data-js-href'));
}

Upvotes: 1

Related Questions