开发者

change domain portion of links with javascript or jquery

Sorry for my original question being unclear, hopefully by reword开发者_StackOverflow中文版ing 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.com and replace with www.domain2.com

i.e. if the user is accessing www.domain2.com/index then:

<a href="www.domain1.com/contentpages/page.html">Test 1</a>

should be rewritten dynamically on load to

<a href="www.domain2.com/contentpages/page.html">Test 1</a>

Is it even possible to rewrite only a portion of the url in an href tag?


Your 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.com', 'rewrittenDomain.com');


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.com/library/blip.htm


Replace urls domains using REGEX

This example will replace all urls using my-domain.com to my-other-domain (both are variables).

You can do dynamic regexs by combining 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.com'
const newDomain = 'my-other-domain.com'

// 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\.com'
console.log('Regex expresion for domain', domain)

// HERE!!! You can 'assemble a complex 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.com to create, test and export REGEX code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜