Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

javascript - 使用Javascript获取当前域名(而不是路径等)(Get The Current Domain Name With Javascript (Not the path, etc.))

I plan on buying two domain names for the same site.

(我计划为同一网站购买两个域名。)

Depending on which domain is used I plan on providing slightly different data on the page.

(根据所使用的域,我计划在页面上提供略有不同的数据。)

Is there a way for me to detect the actual domain name that the page is loading from so that I know what to change my content to?

(有什么方法可以检测页面从中加载的实际域名,以便我知道将内容更改为什么?)

I've looked around for stuff like this but most of it doesn't work the way I want it to.

(我到处寻找这样的东西,但大多数都无法达到我想要的方式。)

For instance when using

(例如当使用)

document.write(document.location)

on JSFiddle it returns

(在JSFiddle上它返回)

http://fiddle.jshell.net/_display/

ie the actual path or whatever that is.

(即实际路径或其他。)

  ask by Freesn?w translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about:

(怎么样:)

window.location.hostname

The location object actually has a number of attributes referring to different parts of the URL

(location对象实际上具有许多属性,这些属性引用了URL的不同部分)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...