js對URL的相關操做集錦

 

 

1.location.href.....php

(1)self.loction.href="/url"html

       window.location.href="/url"    以上兩個用法相同均爲在當前頁面打開URL頁面web

(2)this.location.href="/url"    當前頁面打開URL正則表達式

(3) parent.location.href="/url"   在父頁面打開新頁面,若是頁面中自定義了frame,那麼可將parent self top換爲自定義frame的名稱,效果是在frame窗口打開url地址dom

(4) top.location.href="/url"   在頂層頁面打開新頁面jsp

2. 關於刷新頁面函數

 (1)window.location.href=window.location.hrefpost

 (2)window.location.Reload()ui

  (3)window.location.replace(document.referrer)this

    都是刷新當前頁面。區別在因而否有提交數據。當有提交數據時,window.location.Reload()會提示是否提交,window.location.href=window.location.href;則是向指定的url提交數據

3。返回

  (1)window.history.go(-1)   返回上一頁,go函數內的數字是正表示前進幾頁,是負數表示後退幾頁,history.length判斷歷史幾頁。此種方法不但完成頁面的跳轉,並且刷新頁面

  (2)history.back();  後退 

      history.forward(); 前進   只跳轉,不刷新頁面

4.

 (1)第一段爲實際在用的

1  function getURLParameter(name) {
2 
3      return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null; //構造一個含有目標參數的正則表達式對象
4 
5   }

1  //獲取url中的參數
2         function getUrlParam(name) {
3             var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //構造一個含有目標參數的正則表達式對象
4             var r = window.location.search.substr(1).match(reg);  //匹配目標參數
5             if (r != null) return unescape(r[2]); return null; //返回參數值
6         }

  例如像獲取下面連接的郵箱

  http://agent/index.php/Home/Login/getpwd_check_email?code=824790&to=1321136493@qq.com

   

var mail = getURLParameter('to');

 

JavaScript獲取當前根目錄

 

function getRootPath_web() {
            
            var curWwwPath = window.document.location.href;  //獲取當前網址,如: http://localhost:8083/uimcardprj/share/meun.jsp
            
            var pathName = window.document.location.pathname;  //獲取主機地址以後的目錄,如: uimcardprj/share/meun.jsp
            var pos = curWwwPath.indexOf(pathName);  
           
            var localhostPaht = curWwwPath.substring(0, pos);  //獲取主機地址,如: http://localhost:8083
            
            var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);  //獲取帶"/"的項目名,如:/uimcardprj
            return (localhostPaht + projectName);
        }

 

備註:

 一、Location 對象詳細信息參考w3school http://www.w3school.com.cn/jsref/dom_obj_location.asp

相關文章
相關標籤/搜索