window.location.hash始终显示为空

window.location.hash always shows empty

本文关键字:显示 location hash window      更新时间:2023-09-26

在我的phonegap应用程序中,我更新了我的数据,我有以下代码,因为我得到了window.location.hash(*表示错误行)值将为空。

function init() {
    $("#homePage").live("pageshow", function() {
    getDatas();
});
$("#editPage").live("pageshow", function() {
    ***var loc = window.location.hash;***
    alert("loc" + loc);
    if(loc.indexOf("?") >= 0) {
        var qs = loc.substr(loc.indexOf("?")+1, loc.length);
        var detailId = qs.split("=")[1];
        $("#editFormSubmitButton").attr("disabled", "disabled");
        dbShell.transaction(function(tx) {
            tx.executeSql("select id,name,age,city,occupation from nameDetail where id=?", [detailId], function(tx, results) {
                $("#mId").val(results.rows.item(0).id);
                $("#mName").val(results.rows.item(0).name);
                $("#mAge").val(results.rows.item(0).age);
                $("#mCity").val(results.rows.item(0).city);
                $("#mOccupation").val(results.rows.item(0).occupation);
                $("#editFormSubmitButton").removeAttr("disabled");
            });
        }, dbErrHandler);
    } else {
    alert("empty");
        $("#editFormSubmitButton").removeAttr("disabled");
    }
});
}
function getDatas() {
dbShell.transaction(function(tx) {
    tx.executeSql("select id,name,age,city,occupation,date from nameDetail order by date desc", [], renderEntries, dbErrHandler);
}, dbErrHandler);
}
function renderEntries(tx, results) {
 if (results.rows.length == 0) {
    $("#mainContent").html("<p>Don't have any Details</p>");
} else {
    var s = "";
    for (var i = 0; i < results.rows.length; i++) {
        s += "<li><a href='addEdit.html?id="+results.rows.item(i).id + "'>" +results.rows.item(i).name + "</a></li>";
        alert("" + s);
    }
//alert(S);
    $("#noteTitleList").html(s);
    $("#noteTitleList").listview("refresh");
}
}

Index.html:

<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320; user-scalable=no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Names</title>
<link href="css/jquery.mobile-1.0rc1.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
<script src="js/jquery-1.6.4.js"></script>
<script src="js/jquery.mobile-1.0rc1.min.js"></script>
<script src="js/index.js"></script>
</head>
<body onload="init();">

<div data-role="page" id="homePage">
    <div data-role="header">
        <h1>Names</h1>
    </div>
    <div data-role="content" id="mainContent">  
        <ul data-role="listview" id="noteTitleList"></ul>
    </div>
    <div data-role="footer" class="ui-bar">
        <a href="addEdit.html" data-role="button" data-icon="plus">Add Note</a>
    </div>
</div>
</body>
</html>

和addEdit.html:

<div data-role="page" id="editPage">
    <div data-role="header">
        <h1>Details</h1>
    </div>
    <div data-role="content">   
        <form id="addEditForm" method="post">
            <input type="hidden" name="mId" id="mId" value="">
            <div data-role="fieldcontain">
                <label for="mName">Name</label>
                <input type="text" name="mName" id="mName"/>
            </div>
            <div data-role="fieldcontain">
                <label for="mAge">Age</label>
                <input name="mAge" id="mAge"/>
            </div>
            <div data-role="fieldcontain">
                <label for="mCity">City</label>
                <input name="mCity" id="mCity"/>
            </div>
            <div data-role="fieldcontain">
                <label for="mOccupation">Occupation</label>
                <input name="mOccupation" id="mOccupation"/>
            </div>
            <div data-role="fieldcontain">
                <input type="submit" id="editFormSubmitButton" value="Save Note">
            </div>
        </form>
    </div>
    <div data-role="footer" class="ui-bar">
        <a href="index.html" data-role="button" data-icon="home">Return Home</a>
        <input type="button"  data-role="button" id="sync" name="sync" value="Sync" data-icon="arrow-d"/>
    </div>
</div>

如何解决这个问题一些身体帮助解决这个。。。

编辑:

使用此解决了问题。解决方案链接

我使用以下方法得到了解决方案

var loc = $(location).attr('href');
if (loc.indexOf("?") >= 0) {
    var url = loc.substr(loc.indexOf("?") + 1, loc.length);
    listId = url.split("=")[1];

这对像我这样的人会有帮助。

我使用这个链接解决了我的问题参考

我假设这个javascript的工作方式与传统浏览器上的javascript相同。。

根据代码中的注释判断,您可能希望散列包含查询字符串?

散列只保存url 中散列之后的内容

例如

www.this.com/apage.html?firstval=value&另一个值=页面中的45#位置

在这个url中,

window.locationhash将等于"#locationinpage"

但是

window.location.search将等于"?firstval=value&anothervalue=45"

即:不包括散列后部分的查询字符串

也许你需要window.location.search,或者同样需要?