[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页  
}


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