2014-01-03 6 views
0

Я могу успешно сохранить значения журнала, но имею проблему при отображении зарегистрированных значений на другой странице того же домена. Я сохраняю зарегистрированные значения через localStorage в first.html. Мой код:Значение LocalStorage не отображается на странице HTML

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script> 
$(document).ready(function() { 
     //code of localStorage goes here  
     }) 
</script> 
    </head> 
    <body> 
    <form id="logForm" method="post"> 
     <input type="text" name="project" value="Project Name"> 
     <input type="submit" value="Log Time"> 
     </form> 
    <a href="otherpage.html"> 
     This is a link to otherpage.html 
    </a> 
</body> 
</html> 

Я хочу, чтобы отобразить записанные значения в otherpage.html. Мой код в otherpage.html является:

<!DOCTYPE html> 
<html> 
<body> 
     <p>This page will show the logged values</p> 
     <ul id="theLog"></ul> 
    <button type="button" id="clearLog">Clear Log</button> 
</body> 
</html> 

К сожалению, otherpage.html не показывает ничего, когда я пытаюсь отобразить ul. Может кто-нибудь сказать мне, почему он не отображает anythin или не предлагает какой-либо альтернативный метод для его отображения в otherpage.html. Постскриптум Я не хочу отображать его на одной странице.

+0

Но вы не призывающую 'localStorage' на otherpage.html .... Что вы ожидаете произойдет здесь? –

+0

Как вызвать 'localStorage' на' otherpage.html'? – user2567857

+0

'var myHtml = localStorage ['key']' –

ответ

1

Вам необходимо поставить соответствующую функцию на otherpage.html, страница не может выполнить функцию, существующую на другой странице. Надень otherpage.html:

$(function(){ 
function getAllItems() { 
    var timeLog = ""; //the variable that will hold our html 
    var i = 0; 
    var logLength = localStorage.length-1; //how many items are in the database starting with zero 

    //now we are going to loop through each item in the database 
    for (i = 0; i <= logLength; i++) { 
     //lets setup some variables for the key and values 
     var itemKey = localStorage.key(i); 
     var values = localStorage.getItem(itemKey); 
     values = values.split(";"); //create an array of the values 
     var q = values[0]; 


     //now that we have the item, lets add it as a list item 
     timeLog += '<li><strong>'+q+'</strong>: '; 
    } 

    //if there were no items in the database 
    if (timeLog == "") 
     timeLog = '<li class="empty">Log Currently Empty</li>'; 

    $("#theLog").html(timeLog); //update the ul with the list items 
} 

getAllItems(); 
}); 
+0

Я поместил их в 'script', но ничего не происходит – user2567857

+0

Пожалуйста, убедитесь, что у вас есть это в голове:' ' –

Смежные вопросы