window.location.hash 頁面跳轉,精確定位,實例展示:
(1)、index.phtml,頁面用於傳參
<script id="bb_list_template" type="text/x-dot-template"> <a title="點擊查看寶貝詳情" href="<?php echo APP_WEB_INDEX_ROOT?>/item/itemdetail<?php echo HTML_TYPE;?>?qntag=1#iid=<%=it.num_iid%>&&state=<%=it.status%>&&page=<%=it.page%>" id="title_<%=it.num_iid%>"> <%=it.title%> </a> </script>
(2)、index.js,用於獲取參數
/**
*window.location.hash 定義和用法
*location是javascript里邊管理地址欄的內置對象.
*比如location.href就管理頁面的url,用location.href=url就可以直接將頁面重定向url。
*而location.hash則可以用來獲取或設置頁面的標簽值。比如http://domain/#admin的location.hash="#admin"。
*
*substr 定義和用法
*substr() 方法可在字符串中抽取從 start 下標開始的指定數目的字符。
*語法 stringObject.substr(start,length)
*start 必需。要抽取的子串的起始下標。必須是數值。如果是負數,那么該參數聲明從字符串的尾部開始算起的位置。
*也就是說,-1 指字符串中最后一個字符,-2 指倒數第二個字符,以此類推。
*length 可選。子串中的字符數。必須是數值。如果省略了該參數,那么返回從 stringObject 的開始位置到結尾的字串。
*/
var shash = window.location.hash.substr(1);
var params = shash.split('&');
var map = new HashMap();
for (var i in params) {
var p = params[i].split("=");
if (p.length == 2) {
map.put(p[0], p[1]);
}
}
num_iid = map.get('iid');
detailstate = map.get('state');
if(detailstate =='chuc' || detailstate == 'chus' || detailstate == 'cangk' || detailstate == 'shouw'){
$('#deatailback').show();
}
page = map.get('page');
(3)、返回按鈕用於定位
<a class="sui-btn btn-bordered btn-primary" style="display:none()" id="deatailback" href="javascript:bblist()">返回首頁</a> /** * [bblist 跳首頁] */ function bblist() { document.location.href = APP_WEB_INDEX_ROOT_CDN + 'index' + HTML_TYPE + '?qntag=1#detailstate=' + detailstate + '&page=' + page + '&itemid=' + num_iid; }