[JS]手機端網頁開發問題及解決方法整理

Q1:有時候使用相對路徑無法訪問網站資源文件,需要使用絕對路徑。
A1:

獲取跟路徑

<%
    String path = request.getContextPath();
%>

訪問資源

<link href="<%=path%>/common/css/bootstrap.min.css" rel="stylesheet" media="screen">
<script type="text/javascript" src="<%=path%>/common/js/common.js"></script>

注意:js文件中獲取根路徑

function getRootPath(){
    var curWwwPath=window.document.location.href;
    var pathName=window.document.location.pathname;
    var pos=curWwwPath.indexOf(pathName);
    var localhostPaht=curWwwPath.substring(0,pos);
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
    return(projectName);
}

但是有些上述方法通過解析url獲得根路徑並不是很保險,有時通過不同頁面進入一個頁面時url並不相同,解析失敗。
可以使用以下方法,引入的js文件中可以使用root()獲取根路徑。

function root(){
    return "<%=path%>";
}


Q2:返回上一頁。
A2:

function back(){
    history.go(-1); //後退1頁  
}

對於有些會自動跳轉的頁面需要後退兩頁

function back(){
    history.go(-2); //後退2頁  
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章