Zach Broniszewski
Zach Broniszewski

Reputation: 252

How to append GET values to end of every link on page

I've got a pretty straight forward question, how do I append GET values to the end of every anchor href on a page?

For example, you type in google.com?label=12&id=12

So whatever comes in, I just want to make sure that I'm appending "?label=12&id=12" to the end of every single href in an anchor tag on the page. There is an easy way to do this right? Thanks everyone! :)

Upvotes: 1

Views: 1216

Answers (1)

Bill H
Bill H

Reputation: 590

You can get all the anchors on the page with

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

then iterate through them adding your arguments

for(var i = 0; i< links.length; i++){
    links[i].href = links[i].href + "?label=12&id=12"
}

You may have to do some work to format the parameters correctly to avoid having ?existing=value?label12&id=2

Upvotes: 2

Related Questions