localStorage.setItem()不在页面加载上工作

时间:2018-04-23 17:14:23

标签: javascript

我想加载一个页面,在body标签之后,执行基于localStorage的排序。但myWay(没有双关语意)不起作用。你能救我吗?

我有3个排序函数,每个函数都有以下一行:

<script>

localStorage.setItem("current_sort", "classSort()");
localStorage.setItem("current_sort", "playerSort()");
localStorage.setItem("current_sort", "ratingSort()");

</script>

<script>

myWay = localStorage.getItem("current_sort");

if (myWay === "") {myWay = "playerSort()";}

</script>

如何加载myway?

<script>
eval (myway()); //doesn't seem to work.
</script>

1 个答案:

答案 0 :(得分:-3)

这就是我们如何将函数作为第二个参数执行的:

localStorage.setItem("current_sort", function(){classSort()});
localStorage.setItem("current_sort", function(){nameSort()});
localStorage.setItem("current_sort", function(){ratingsort()});