Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript或jquery更改链接的域部分_Javascript_Jquery_Hyperlink - Fatal编程技术网

使用javascript或jquery更改链接的域部分

使用javascript或jquery更改链接的域部分,javascript,jquery,hyperlink,Javascript,Jquery,Hyperlink,很抱歉,我原来的问题不清楚,希望通过重写,我可以更好地解释我想做什么 因此,我需要一种使用JavaScript(或jQuery)执行以下操作的方法: 确定正在访问的当前页面的域 识别页面上使用域www.domain1.com的所有链接,并替换为www.domain2.com i、 e.如果用户正在访问www.domain2.com/index,则: <a href="www.domain1.com/contentpages/page.html">Test 1</a>

很抱歉,我原来的问题不清楚,希望通过重写,我可以更好地解释我想做什么

因此,我需要一种使用JavaScript(或jQuery)执行以下操作的方法:

  • 确定正在访问的当前页面的域
  • 识别页面上使用域www.domain1.com的所有链接,并替换为www.domain2.com
i、 e.如果用户正在访问www.domain2.com/index,则:

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

应在加载到时动态重写

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


甚至可以在
href
标记中只重写url的一部分吗?

您需要使用Java或其他服务器端的东西才能获得IP地址。见此:


我想出了如何让它工作

<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>

//链接重写器
$(文件)。准备好了吗(
函数链接\重写器(){
var hostadd=location.host;
var供应商='999.99.999.9';
var localaccess='somesite1';
如果(主机添加==供应商){
$(“a”)。每个(函数(){
var o=$(本);
var href=o.attr('href');
var-newhref;
newhref=href.replace(/somesite1/i,“999.99.999.99”);
o、 attr('href',newhref);
});
}
}
);

您的代码将在页面上的所有链接上循环。这是一个只对需要替换的URL进行迭代的版本

var linkRewriter = function(a, b) {
    $('a[href*="' + a + '"]').each(function() {
        $(this).attr('href', $(this).attr('href').replace(a, b));
    });
};

linkRewriter('originalDomain.com', 'rewrittenDomain.com');
使用正则表达式替换URL域 此示例将使用
my domain.com
将所有URL替换为
my other domain
(两者都是变量)

可以通过在原始字符串模板中组合字符串值和其他正则表达式来执行动态正则表达式。使用
String.raw
将阻止javascript转义字符串值中的任何字符

// 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);
注意:不要忘记使用regex101.com创建、测试和导出REGEX代码


我们谈论的是多少用户?例如,如果是呼叫中心楼层,我们谈论的是很多IP地址。没有其他不同的方式来区分这些用户吗?欢迎使用SO-有一些漂亮的布局控件,不需要您使用HTML进行格式化。我刚刚重新格式化了基本问题代码。谢谢也。。。为什么不使用相对路径呢?检测用户的ip是最简单的部分。因为他们通过隧道访问,所以我只需要查找1个ip,我可以在当前url的主机部分找到它,并使用location.host进行访问。我正在努力解决的部分是如何识别页面上使用“example.com”的所有链接,并将该部分超链接替换为“xxx.xx.xxx”。。。我不能使用相对链接,因为网站的结构很糟糕(我无法控制)@mpriney:你是说在服务器端这样做吗?
javascript
标记使它看起来像是在考虑客户端。如果是客户端,那么用户IP就不容易了。我使用它通过调用
linkRewriter('http://foo.bar/','/');如果我使用相对域并希望更新该域如何?