是否有一种简单的方法从URL字符串中获取网站名称?如何从Javascript中获取网站名称?
实施例:
http://www.mysite.com/mypath/mypage -> www.mysite.com
http://mysite.com/mypath/mypage -> mysite.com
的JS代码上的mongodb CLI一侧执行,在浏览器中不。
是否有一种简单的方法从URL字符串中获取网站名称?如何从Javascript中获取网站名称?
实施例:
http://www.mysite.com/mypath/mypage -> www.mysite.com
http://mysite.com/mypath/mypage -> mysite.com
的JS代码上的mongodb CLI一侧执行,在浏览器中不。
这一个运作良好,对我来说:
urlString.split( '/')[2]
简单。
window.location.hostname; //Domain name
$("title").text(); //Page name
这也太
var loc = window.location;
var filename = loc.pathname.split("/");
filename = filename[pathname.length-1];
alert("Domain: "+loc.hostname);
alert("Filename: "+filename);
试试这个:
url.href = "http://www.mysite.com/mypath/mypag";
url.protocol; // => "http:"
url.hostname; // => "example.com" // site name, you want
的window.document.location.href
有页面的URL和window.document.location.hostname
将站点名称。
所以,
console.log(window.document.location.hostname); // will log the sitename in the console
你可以为了得到你想要的数据拖出网址的滥用锚。 :d
function getDomain(url) {
var anchor = document.createElement('a');
anchor.setAttribute('href', url);
return anchor.hostname;
}
console.log(getDomain('http://www.mysite.com/mypath/mypage'));
console.log(getDomain('http://mysite.com/mypath/mypage'));
问题是关于服务器端JS(mongodb/node.js等) - 我的问题是由别人编辑,回滚 – BreakPhreak
我能问,这是为什么在执行MongoDB CLI?在node.js的文档插入中做这件事情会不会更谨慎?或者在查询它? – Sammaye
它是MongoDB中的一个“组”查询的一部分。 – BreakPhreak
你能告诉我们你对这个组查询atm有什么吗?这样做听起来很好,更重要的是,如果您经常在网站名称上查询(分组),您会希望将该网站名称的文档保存为预定义的,因为即使您可以在聚合框架中将其正常化部分它可能永远不会使用索引,这将是污垢慢 – Sammaye