MB.
MB.

Reputation: 4211

javascript apply #hash to all links

How can I append #somehash to all links on a page without depending on a JS framework?

Or is it possible to catch when someone clicks on a link and attach #somehash?

Upvotes: 0

Views: 358

Answers (2)

roryf
roryf

Reputation: 30160

As pointed out, you're probably doing this the wrong way, but...

var links = document.getElementsByTagName('a');

for(var i = 0; i < links.length; i++) {
    var link = links[i];
    if (link.href.indexOf('#') < 0) {
        link.href += '#somehash';
    }
}

Upvotes: 3

psychotik
psychotik

Reputation: 39019

Make the server add hashed?

Otherwise you will need JS. PS, how do you plan to do anything useful with the hashes without JS enabled?

Upvotes: 1

Related Questions