2017-02-25 69 views
-2

我想从文件链接(http://www.example.com/file.exehttps://example.com/folder/file.txt#someValue?params)获得没有http(s)://www.的纯文本域(例如,脚本的结果为example.com)。使用Javascript或jQuery获取文件服务器域名

我如何使用js或jquery做到这一点?

回答

3

这可以用纯javascript完成。

var url = new URL("https://example.com/folder/file.txt#someshit?someothershit"); 
 
var hostname = url.hostname.replace("www.", ""); 
 
console.log(hostname);

+0

注意,' “WWW。” 代替"www."'是的一部分'.hostname' – guest271314

1

您可以使用URL()构造,.hostname财产,与空字符串

var url = new URL("http://www.example.com/file.exe") 
 
      .hostname.replace(/www\./, ""); 
 
      
 
console.log(url);