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);
});
javascript local-storage
add a comment |
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);
});
javascript local-storage
1
Are you sure you're callinginit_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
add a comment |
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);
});
javascript local-storage
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
javascript local-storage
edited Nov 5 at 2:12
asked Nov 5 at 2:07
Joy
93
93
1
Are you sure you're callinginit_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
add a comment |
1
Are you sure you're callinginit_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
add a comment |
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);
});
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
add a comment |
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);
});
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
add a comment |
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);
});
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
add a comment |
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);
});
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);
});
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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