0

how can I replace all url's in a page with different URL using jquery ? thanks :)

3 Answers 3

2

Found this:

//Create an object of all links
var links = $('a');
//Parse each item in links object
for (var a in links){
//This will allow the for iteration to give the actual link objects that are
//referred to with numeric indexes and not objects that jQuery appends
//Object 'a' should be a number
    if(a == parseInt(a)){
        //Variable b is now the object that is links[a];
        var b = links[a];
        //Variable c is now variable b cast to jQuery so I can use built in jQuery functions
        var c = $(b);
        //Variable temp now contains the href of that link
        var temp = c.attr('href');
        //This should filter out any anchors in the page or any links without an href
        if(temp != undefined){
            //This checks to see if they are inline links, mailto link, OR absolute link
            //This isn't perfect in the case that your link was 'mailsomething.php' or any non http link (ftp or other protocol)
            //The correct scenario here is to use regex but I didn't have the patience
            //or time to do so, so I didn't plus I knew my links didn't apply to these caveats
            var test = temp.substring(0,4);
            if(test != 'mail' && test != 'http' && test != '#'){
                //Now we prepend the abosulte url with the proper and add the relative file location
                c.attr('href','http://differentsubdomain.domain.com/'+temp);
            }
        }
    }
}

on this page:

Rewrite all links on a page...

Sign up to request clarification or add additional context in comments.

1 Comment

If it works, then you should award him the correct answer by clicking the checkmark.
1

Here's an example:

$("a").attr("href","anotherurl.html");

Comments

0

This will select all anchors in the page: $('a')

This will set the href attribute: $('a').attr('href','')

I'm not sure what that would be needed for, tho.

Comments

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.