我有一個url變量http://blah.com/blah/blah/blah
,我有另一個URL http://shop.blah.com/
我要採取的第一個URL(blah.com),並添加結束blah/blah/blah
第二url http://shop.blah.com
jQuery附加url的結尾?
所以我最終http://shop.blah.com/blah/blah/blah
任何想法我怎麼能做到這一點?
我有一個url變量http://blah.com/blah/blah/blah
,我有另一個URL http://shop.blah.com/
我要採取的第一個URL(blah.com),並添加結束blah/blah/blah
第二url http://shop.blah.com
jQuery附加url的結尾?
所以我最終http://shop.blah.com/blah/blah/blah
任何想法我怎麼能做到這一點?
var url1 = 'http://blah.com/blah/blah/blah';
var url2 = 'http://shop.blah.com/';
var newUrl = url2 + url1.replace(/^.+?\..+?\//, '');
這聽起來像jQuery-URL-Parser插件可能在這裏派上用場:
var url = $.url(); //retrieves current url
您還可以得到URL的特定部分是這樣的:
var file = $.url.attr("file");
var path = $.url.attr("path");
var host = $.url.attr("host");
...
如果你需要得到查詢字符串參數:
var parm = $.url.param("id");
如果目的只是爲了增加shop
到域名的前面:
var url2 = url1.replace('://', '://shop.');
<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>
是輸入域部分不變,或者它可能包含其他的東西? – Alnitak