0

I am not sure if there is a name for what I want to do, but I would like to use javascript to alter every url on a webpage on my website. So if someone posts a url such as:

visit http://www.blah.com

when viewing this page, I want javascript to change it to:

visit http://www.mysite.com/count.php?out=http://www.blah.com

I know about the two urls in one I will handle that part, I just used this as an example of placing the same text in front of every url in the page. Kinda like how thewaybackmachine does it. Thanks.

4
  • Javascript can do this. What exactly is your question? Commented Apr 6, 2014 at 6:00
  • 1
    The url you gave is an PHP GET url. Why do you want to use javascript for this? Commented Apr 6, 2014 at 6:01
  • Darn sorry you are right, I just found out that php works. I am searching for a PHP way to do this now. Commented Apr 6, 2014 at 6:04
  • If you have something like page.php?id=test, you can access the sent information with $_GET['id'] inside your PHP code Commented Apr 6, 2014 at 6:08

2 Answers 2

3

Using jQuery:

$("a").attr('href', "http://test.com?blah=" + $(this).attr('href'));

This code searches for all a tags on the page, and replaces the contents of the href attribute with http://test.com?blah= followed by the original contents of the href attribute.

To try it out, You can copy it, hit 'f12', paste it into the console, hit enter, and see it's effects on this page right here! (also breaking all your links...)

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

5 Comments

OP didn't tag it jquery
I'd recommend that if you're not proficient in JavaScript, you get jQuery and use it and learn it.
@DeanRather Could not agree with you more. And, wow, I literally just typed the same thing. Oh well.
@DeanRather I would recommend forget about jQuery before you learn how to do that without it.
Dean, you are right for copy - pasters. @dfsq, your statement is valid for developers.
0

Here's a way, without the need for JQuery. (loading and parsing a pretty big library in order to just do one simple thing may be a bit much..)

[].slice.call(document.querySelectorAll('a')).map(function(a){ a.href = 'http://somesite.com/count.php?' + a.href }) 

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.