2011-07-12 32 views
1

我應該打印網址在正在建設中的頁面。 但是,如果我得到的網址W/乾淨的URL,然後打印在javascript

var url = window.location; 
document.write(" <p>"+url+"</p>"); 

我得到這樣的 「http://mypage.ass/trololo/pageasd/.../index.html」

但我需要簡單地「 mypage.ass「,沒有別的。

我試圖與子清理,但每一個瀏覽器說,這是不是一個函數。 -.-

回答

1

你只需要拆分的網址:

var localUrl = document.URL 
localUrl = localUrl.split('http://')[1].split('/'); 
document.write("<p>"+localUrl[0]+"</p>"); 

BTW:用戶可以document.URL避免混淆,當你的服務重定向

+0

謝謝你,完美。 :) – appl3r

1

使用window.location.hostname代替window.location。後者沒有.split

var myass = window.location.hostname; 
-1

如果你的域名是3字符長,這可能工作:

var url = "mypage.ass/ilikepies"; 
url.substring(url.indexOf(":") + 2, url.indexOf(".") + 3) 
+0

這並沒有真正意義,因爲它只有在域長度是靜態的工作 - 在這種情況下,你也可以直接硬編碼域本身... – pimvdb