javascript 中的location.pathname

來源:互聯網
上載者:User

location.pathname: 返回URL的網域名稱(網域名稱IP)後的部分。

例如 http://www.example.com/wordpress/返回/wordpress/,
又或則 http://127.0.0.1/index.html 返回/index.html,
注意是帶url的網域名稱或網域名稱IP

在磁碟上隨便建個Html檔案進行location.pathname測試,如瀏覽器上的路徑是: C:\Documents and Settings\Administrator\案頭\testjs.html, 這樣,得到的結果是: /C:\Documents and Settings\Administrator\案頭\testjs.html

既然提到這了,那我們就分析下下面的URL:

http://www.example.com:8080/test.php?user=admin&pwd=admin#login

想得到整個如上的完整url,我們用:location.href; 
得到傳輸協議http:,我們用:location.protocol; 
得到主機名稱連同連接埠www.example.com:8080,我們用:location.host; 
得到主機名稱www.joymood.cn,我們用:location.hostname; 
得到主機後部分不包括問號?後部分的/test.php,就用我們剛才講的:location.pathname; 
得到url中問號?之後井號#之前的部分?user=admin&pwd=admin,我們就用: location.search; 
得到#之前的部分#login,我們就用location.hash

如上,我們可以通過location對象的某些屬性得到一個完整URL的各個部分。

http://hi.baidu.com/xiaoxie336l/item/8628a634fdc3a5d996f88d70

 

 

    this.getContextPath = function() {
        var path;
        if(location.pathname.indexOf("/")==0){
            path=location.pathname.substring(1);
        }else{
            path=location.pathname;
        }

        var index=path.indexOf("/");
        var contextPath="/"+path.substring(0,index);

        return contextPath;
    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.