I have following URL in my few pages(http://something.com)..
<a href="http://something.com">Home</a>
<a href="index.html">Index</a>
<a href="about.html">About</a>
<a href="contact.php">Contact</a>
<a href="#">Same</a>
<a hre="http://example.com/home.html">New Home</a>
<a href="../services.html">Services</a>
and what I want is to convert all link to…
<a href="http://this.com/?url=http://something.com">Home</a>
<a href="http://this.com/?url=http://something.com/index.html">Index</a>
<a href="http://this.com/?url=http://something.com/about.html">About</a>
<a href="http://this.com/?url=http://something.com/contact.php">Contact</a>
<a href="#">Same</a>
<a hre="http://this.com/?url=http://example.com/home.html">New Home</a>
<a href="../services.html">Services</a>
So Basically I don’t want to Convert "#" or "../" such link.
I am noob in JS.
From my effort, with the help of w3schools.. What i have tried to accomplish :-
<script type="text/javascript">
var url= document.getElementByTagName(a);
var pattern = /..\//g;
var result = pattern.test(url);
if(url.href != "#" || result === "false" ) {
var nurl = url.replace("http://this.com/?url="+url.href, url.href);
}
</script>
And I am not able to do anything… Please help, how can I modify URL and add http://this.com/?url=My_web_page_url_here.
UPDATE
I replaced my javascript with
<script type="text/javascript">
var links = document.links;
var i = links.length;
while (i--) {
if (links[i].href.slice(-1) == "#" ||
links[i].getAttribute("href").slice(0, 3) == "../") {
continue;
}
links[i].href = "http://this.com/?url=" + encodeURIComponent(links[i].href);
}
</script>
And still all url’s are in the same way without append.
Try this…
jsFiddle.
I encoded the parameter, but if you don’t want it encoded, like in your examples, drop the call to
encodeURIComponent().