localStorage getItem is null, after setItem











up vote
-1
down vote

favorite












When load the page, will call the init_table function and the "#csv" btn will bind a click function. After clicking the #csv and print the 'export2csv' in localStorage, it is null, but sometimes it works, why?



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});









share|improve this question




















  • 1




    Are you sure you're calling init_table() before the button is clicked?
    – weirdpanda
    Nov 5 at 2:16










  • "Sometimes it works" questions means you miss some debugging steps. So double check every bit of code that do access the localStorage. Possible causes are Incognito mode, localStorage.clear() , localStorage.removeItem('export2csv'), delete localStorage.export2csv, extensions.
    – Kaiido
    Nov 5 at 2:43

















up vote
-1
down vote

favorite












When load the page, will call the init_table function and the "#csv" btn will bind a click function. After clicking the #csv and print the 'export2csv' in localStorage, it is null, but sometimes it works, why?



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});









share|improve this question




















  • 1




    Are you sure you're calling init_table() before the button is clicked?
    – weirdpanda
    Nov 5 at 2:16










  • "Sometimes it works" questions means you miss some debugging steps. So double check every bit of code that do access the localStorage. Possible causes are Incognito mode, localStorage.clear() , localStorage.removeItem('export2csv'), delete localStorage.export2csv, extensions.
    – Kaiido
    Nov 5 at 2:43















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











When load the page, will call the init_table function and the "#csv" btn will bind a click function. After clicking the #csv and print the 'export2csv' in localStorage, it is null, but sometimes it works, why?



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});









share|improve this question















When load the page, will call the init_table function and the "#csv" btn will bind a click function. After clicking the #csv and print the 'export2csv' in localStorage, it is null, but sometimes it works, why?



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});






javascript local-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 2:12

























asked Nov 5 at 2:07









Joy

93




93








  • 1




    Are you sure you're calling init_table() before the button is clicked?
    – weirdpanda
    Nov 5 at 2:16










  • "Sometimes it works" questions means you miss some debugging steps. So double check every bit of code that do access the localStorage. Possible causes are Incognito mode, localStorage.clear() , localStorage.removeItem('export2csv'), delete localStorage.export2csv, extensions.
    – Kaiido
    Nov 5 at 2:43
















  • 1




    Are you sure you're calling init_table() before the button is clicked?
    – weirdpanda
    Nov 5 at 2:16










  • "Sometimes it works" questions means you miss some debugging steps. So double check every bit of code that do access the localStorage. Possible causes are Incognito mode, localStorage.clear() , localStorage.removeItem('export2csv'), delete localStorage.export2csv, extensions.
    – Kaiido
    Nov 5 at 2:43










1




1




Are you sure you're calling init_table() before the button is clicked?
– weirdpanda
Nov 5 at 2:16




Are you sure you're calling init_table() before the button is clicked?
– weirdpanda
Nov 5 at 2:16












"Sometimes it works" questions means you miss some debugging steps. So double check every bit of code that do access the localStorage. Possible causes are Incognito mode, localStorage.clear() , localStorage.removeItem('export2csv'), delete localStorage.export2csv, extensions.
– Kaiido
Nov 5 at 2:43






"Sometimes it works" questions means you miss some debugging steps. So double check every bit of code that do access the localStorage. Possible causes are Incognito mode, localStorage.clear() , localStorage.removeItem('export2csv'), delete localStorage.export2csv, extensions.
– Kaiido
Nov 5 at 2:43














1 Answer
1






active

oldest

votes

















up vote
-1
down vote













You need to call the init_table function on page load, just it sitting there won't call it by itself :)



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}
// Call the init_table function when the page loads
init_table();

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});





share|improve this answer





















  • Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
    – Mike 'Pomax' Kamermans
    Nov 5 at 2:28












  • What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
    – Kaiido
    Nov 5 at 2:39












  • @Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
    – Petar
    Nov 5 at 11:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147417%2flocalstorage-getitem-is-null-after-setitem%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
-1
down vote













You need to call the init_table function on page load, just it sitting there won't call it by itself :)



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}
// Call the init_table function when the page loads
init_table();

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});





share|improve this answer





















  • Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
    – Mike 'Pomax' Kamermans
    Nov 5 at 2:28












  • What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
    – Kaiido
    Nov 5 at 2:39












  • @Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
    – Petar
    Nov 5 at 11:03















up vote
-1
down vote













You need to call the init_table function on page load, just it sitting there won't call it by itself :)



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}
// Call the init_table function when the page loads
init_table();

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});





share|improve this answer





















  • Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
    – Mike 'Pomax' Kamermans
    Nov 5 at 2:28












  • What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
    – Kaiido
    Nov 5 at 2:39












  • @Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
    – Petar
    Nov 5 at 11:03













up vote
-1
down vote










up vote
-1
down vote









You need to call the init_table function on page load, just it sitting there won't call it by itself :)



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}
// Call the init_table function when the page loads
init_table();

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});





share|improve this answer












You need to call the init_table function on page load, just it sitting there won't call it by itself :)



function init_table() {
var url = 'http://localhost:3000/api/feature/featureInFbpGet?nameInFB=UL PHY';
localStorage.setItem('export2csv', url);
console.log(localStorage.getItem('export2csv'));//could print the value
table = $('#example').DataTable({
ajax:{url: url}}).draw();
//...... some operation
});
}
// Call the init_table function when the page loads
init_table();

$('#csv').click(function(e){
e.preventDefault();
console.log('2.export2csv', localStorage.getItem('export2csv'));// here will print null
var opencsv = window.open('./html/other/feature_statement_csv.html');
// setTimeout(function(){opencsv.close();}, 5000);
});






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 5 at 2:22









Petar

230110




230110












  • Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
    – Mike 'Pomax' Kamermans
    Nov 5 at 2:28












  • What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
    – Kaiido
    Nov 5 at 2:39












  • @Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
    – Petar
    Nov 5 at 11:03


















  • Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
    – Mike 'Pomax' Kamermans
    Nov 5 at 2:28












  • What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
    – Kaiido
    Nov 5 at 2:39












  • @Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
    – Petar
    Nov 5 at 11:03
















Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
– Mike 'Pomax' Kamermans
Nov 5 at 2:28






Your answer and your code don't agree.... Now you're just running init_table immediately instead of at page load. If the idea is actually run it at page load, use $(function init_table() { ... }), to take advantage of jQuery's "run this on page load" functionality. (Where $(...) is a convenient shorthand for $(document).ready(...))
– Mike 'Pomax' Kamermans
Nov 5 at 2:28














What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
– Kaiido
Nov 5 at 2:39






What is the point of using LocaleStorage if the value you are reading there gets overridden at page load anyway?
– Kaiido
Nov 5 at 2:39














@Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
– Petar
Nov 5 at 11:03




@Kaiido yea guys you're right, my bad, shouldn't have rushed it with the answer. Although, his question says: "when load the page, will call the init_table()", so that wasn't very clear and it got me thinking he wants to run it as the page loads, and also he didn't say he wants to run it on a click or some other kind of event. My bad again, now that I look what I wrote it really doesn't makes sense.
– Petar
Nov 5 at 11:03


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147417%2flocalstorage-getitem-is-null-after-setitem%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini