天天看點

JavaScript 擷取目前協定、域名+端口

​​線上測試​​

網站協定(http、https)

<script> 
    // document.location.protocol
    // 傳回:http:  https:  (注意含“:”字元)
    alert(window.location.protocol);
    alert(window.location.protocol.split(':')[0]); //不含“:”字元
</script>      
<script> 
    // document.location.host
    // 傳回:www.domain.com:8080  (注意:80端口時,隻顯示域名)
    alert(window.location.host);
</script>      
<script> 
    // 1、document.location.hostname
    // 2、document.domain
    // 傳回:www.domain.com  (1、2結果一樣)
    alert(document.domain);
    alert(document.location.hostname);
</script>      
<script> 
    // window.location.protocol(網站協定:https、http)
    // window.location.host    (端口号+域名;注意:80端口,隻顯示域名)
    // 傳回:https://www.domain.com:8080
    var path= window.location.protocol+'//' + window.location.host
    alert(path);
</script>      

繼續閱讀