Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add hash prefix to href description (innerHtml) that are inside a specific div
- =================================================================
- // script.js
- function addHashLabels() {
- const divprobesrv = document.getElementById('probeserver');
- //const links = document.getElementsByTagName('a');
- const links = divprobesrv.getElementsByTagName('a');
- const hashRange = 99 - 1 + 1; // 01 to 99
- for (let i = 0; i < links.length; i++) {
- const url = links[i].getAttribute('href');
- const hash = getHash(url);
- // Generate prefix based on hash number
- const prefixletter=links[i].innerHTML.charAt(0);
- //console.log("InnetHTML="+prefixletter+ " , innerHTML="+links[i].innerHTML)
- const prefix = prefixletter+('0' + (hash % hashRange + 1)).slice(-2);
- // Add prefix to link description
- links[i].innerHTML = prefix + ': ' + links[i].innerHTML;
- }
- };
- // Function to generate hash from URL
- function getHash(url) {
- let hash = 0;
- for (let i = 0; i < url.length; i++) {
- hash = ((hash << 5) - hash) + url.charCodeAt(i);
- hash = hash & hash; // Convert to 32bit integer
- }
- return Math.abs(hash);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement