0

My problem is the following:

I have a page with many links Some of them have a specific pattern :

http://www.example.com/.../?parameter1=...&parameter2=PARAMETER2

What i want to do is to change these links' href to the value of the parameter2 using JavaScript. For example if i have a link like :

<a href="http://www.example.com/.../?parameter1=...&parameter2=PARAMETER2">text here</a> 

what i want to do after the script runs is to have a link like this:

<a href="PARAMETER2">text here</a>

Any suggestion would be truly appreciated!!! Thank you all in advance!!!

1

2 Answers 2

1

If you are using jquery

then use the following code

$(function() {
  $("a[href^='www.example.com']").each(function(){
    var ele = $(this);
    var href = ele.attr("href");console.log(href);
    var index = href.lastIndexOf("parameter2");
    var param_2 = href.substring((index + 11));
    ele.attr("href", param_2);
  });
});
Sign up to request clarification or add additional context in comments.

2 Comments

thank you for your answer..your code works fine but it changes all links.. Is there a way to define to change only for example www.example.com links???
i have changed the post.Please try that!
1

http://jsfiddle.net/LVNeC/

function getUrlVars(_url)
{
    var vars = [], hash;
    var hashes = _url.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

var myLINK = document.getElementById("mylink");
var url = myLINK.href;
myLINK.href = getUrlVars(url )["parameter2"];​

1 Comment

thank you for your answer..I'll try it to see how it works and I'll provide a feedback..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.