Sorry for my original question being unclear, hopefully by rewording I can better explain what I want to do.
Because of this I need a way to use JavaScript (or jQuery) to do the following:
- determine domain of the current page being accessed
- identify all the links on the page that use the domain www.domain1 and replace with www.domain2
i.e. if the user is accessing www.domain2/index then:
<a href="www.domain1/contentpages/page.html">Test 1</a>
should be rewritten dynamically on load to
<a href="www.domain2/contentpages/page.html">Test 1</a>
Is it even possible to rewrite only a portion of the url in an href
tag?
Sorry for my original question being unclear, hopefully by rewording I can better explain what I want to do.
Because of this I need a way to use JavaScript (or jQuery) to do the following:
- determine domain of the current page being accessed
- identify all the links on the page that use the domain www.domain1. and replace with www.domain2.
i.e. if the user is accessing www.domain2./index then:
<a href="www.domain1./contentpages/page.html">Test 1</a>
should be rewritten dynamically on load to
<a href="www.domain2./contentpages/page.html">Test 1</a>
Is it even possible to rewrite only a portion of the url in an href
tag?
- how many users are we talking about? For instance, if it's a call center floor we're talking about a lot of IP addresses. Is there no other distinct way of differentiating these users? – Joseph Marikle Commented Sep 16, 2011 at 19:36
- Wele to SO - there are nifty layout controls which don't require you to use HTML to do formatting. I just reformatted the underlying question code. Thanks! – pimvdb Commented Sep 16, 2011 at 19:36
- Also... why not just use relative paths? – Joseph Marikle Commented Sep 16, 2011 at 19:40
- detecting the user's ip is the easy part. since they access via a tunnel there will only be 1 ip i have to look for and i can find that in the host part of the current url being accessed with location.host. the part that i'm struggling with is how to identify all the links on the page that use "example." and replace that portion of the hyperlink with "xxx.xx.xxx"... I can't use relative links because of the horrible structure of the site(which i have no control over) – mpriney Commented Sep 16, 2011 at 19:43
-
@mpriney: Are you talking about doing this on the server-side? The
javascript
tag makes it look like you're thinking client-side. If client-side, the user IP is not easy. – Jonathan M Commented Sep 16, 2011 at 19:45
4 Answers
Reset to default 12Your code will loop over all links on the page. Here's a version that only iterates over URLS that need to be replaced.
var linkRewriter = function(a, b) {
$('a[href*="' + a + '"]').each(function() {
$(this).attr('href', $(this).attr('href').replace(a, b));
});
};
linkRewriter('originalDomain.', 'rewrittenDomain.');
I figured out how to make this work.
<script type="text/javascript">
// link rewriter
$(document).ready (
function link_rewriter(){
var hostadd = location.host;
var vendor = '999.99.999.9';
var localaccess = 'somesite1.';
if (hostadd == vendor) {
$("a").each(function(){
var o = $(this);
var href = o.attr('href');
var newhref;
newhref = href.replace(/somesite1/i, "999.99.999.99");
o.attr('href',newhref);
});
}
}
);
</script>
You'll need to involve Java or something server-side to get the IP address. See this:
http://javascript.about./library/blip.htm
Replace urls domains using REGEX
This example will replace all urls using my-domain.
to my-other-domain
(both are variables).
You can do dynamic regexs by bining string values and other regex expressions within a raw string template. Using String.raw
will prevent javascript from escaping any character within your string values.
// Strings with some data
const domainStr = 'my-domain.'
const newDomain = 'my-other-domain.'
// Make sure your string is regex friendly
// This will replace dots for '\'.
const regexUrl = /\./gm;
const substr = `\\\.`;
const domain = domainStr.replace(regexUrl, substr);
// domain is a regex friendly string: 'my-domain\.'
console.log('Regex expresion for domain', domain)
// HERE!!! You can 'assemble a plex regex using string pieces.
const re = new RegExp( String.raw `([\'|\"]https:\/\/)(${domain})(\S+[\'|\"])`, 'gm');
// now I'll use the regex expression groups to replace the domain
const domainSubst = `$1${newDomain}$3`;
// const page contains all the html text
const result = page.replace(re, domainSubst);
note: Don't forget to use regex101. to create, test and export REGEX code.