Javascript-复制链接

Javascript-复制链接,javascript,html,css,Javascript,Html,Css,我想提取网站地址的一部分并将其附加到其他链接 示例:website.com/abc123->otherwebsite.com/abc123 有没有一个简单的方法可以做到这一点? 我是JavaScript新手,不清楚函数应该在什么环境下运行。 function OtherURL(What) { NewURL='otherwebsite.com'; return NewURL+'\/'+What.substring(What.lastIndexOf('\/')); } 在node.js中,

我想提取网站地址的一部分并将其附加到其他链接

示例:website.com/abc123->otherwebsite.com/abc123

有没有一个简单的方法可以做到这一点?
我是JavaScript新手,不清楚函数应该在什么环境下运行。
function OtherURL(What) {
  NewURL='otherwebsite.com';
  return NewURL+'\/'+What.substring(What.lastIndexOf('\/'));
}
在node.js中,您可以执行以下操作:

var transformUrl = function (url, newDomain) { 
    if (! url.match(/https?:\/\//i)) {
        // prepend URL with protocol if missing 
        url = "http://" + url; 
    } 
    return newDomain + require("url").parse(url).path; 
};

// can be called like this:
// this will return 'otherwebsite.com/abc123'
transformUrl("website.com/abc123", "otherwebsite.com");
transformUrl("http://website.com/abc123", "otherwebsite.com");

请提供更多详细信息您需要哪些详细信息?您希望您的代码在浏览器或其他地方运行吗?如果是浏览器,是否要读取当前窗口URL,或对任何潜在URL应用转换?最后,转换是关于从一个URL复制路径部分并将其附加到另一个域吗?还是有不同的转换?到现在为止,你已经得到了3个问题的答案。其中有一个能解决你的问题吗?如果是,您介意接受/投票吗?
var transformUrl = function (url, newDomain) { 
    if (! url.match(/https?:\/\//i)) {
        // prepend URL with protocol if missing 
        url = "http://" + url; 
    } 
    return newDomain + require("url").parse(url).path; 
};

// can be called like this:
// this will return 'otherwebsite.com/abc123'
transformUrl("website.com/abc123", "otherwebsite.com");
transformUrl("http://website.com/abc123", "otherwebsite.com");