我有一个网址变量http://blah.com/blah/blah/blah
,我有另一个网址http://shop.blah.com/
我想要获取第一个网址(blah.com)并将结尾blah/blah/blah
添加到第二个网址{{ 1}}
所以我最终得到http://shop.blah.com
我知道如何做到这一点吗?
答案 0 :(得分:1)
var url1 = 'http://blah.com/blah/blah/blah';
var url2 = 'http://shop.blah.com/';
var newUrl = url2 + url1.replace(/^.+?\..+?\//, '');
答案 1 :(得分:0)
听起来jQuery-URL-Parser插件可能会派上用场:
var url = $.url(); //retrieves current url
您还可以像这样获取网址的特定部分:
var file = $.url.attr("file");
var path = $.url.attr("path");
var host = $.url.attr("host");
...
如果您需要获取Querystring参数:
var parm = $.url.param("id");
答案 2 :(得分:0)
如果目的只是将shop
添加到域名的前面:
var url2 = url1.replace('://', '://shop.');
答案 3 :(得分:0)
<script>
$(document).ready(function(){
//this uses the browser to create an anchor
var blah = document.createElement("a");
//initialize the anchor (all parts of the href are now initialized)
blah.href = "http://blah.com/blah/blah/blah?moreBlah=helloWorld#hashMark";
var shop = document.createElement("a");
//initialize the anchor (all parts of the href are now initialized)
shop.href = "http://shop.blah.com/";
shop.pathname = blah.pathname; //the blahs
shop.search = blah.search; //the blah query
shop.hash = blah.hash; // the blah hashMark
alert("These are the droids you're looking for: "+shop.href);
});
</script>