Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据推送到javascript数组,但数组没有增长_Javascript_Html_Url_Push - Fatal编程技术网

将数据推送到javascript数组,但数组没有增长

将数据推送到javascript数组,但数组没有增长,javascript,html,url,push,Javascript,Html,Url,Push,我正在尝试为一个内部网站创建一个导航系统 为此,我通过javascript创建了一个数组来跟踪页面的url,并在每个新页面中将新url推送到数组中 问题是,每个新页面似乎都覆盖了最后一页 这是我的javascript文件中的内容。。。请注意,我仅在阵列不存在时创建一个新阵列(该阵列将在用户离开网站时被删除) 这是我用来查看html中的值的 <script type="text/javascript"> <!-- document.write(last_element);

我正在尝试为一个内部网站创建一个导航系统

为此,我通过javascript创建了一个数组来跟踪页面的url,并在每个新页面中将新url推送到数组中

问题是,每个新页面似乎都覆盖了最后一页

这是我的javascript文件中的内容。。。请注意,我仅在阵列不存在时创建一个新阵列(该阵列将在用户离开网站时被删除)

这是我用来查看html中的值的

<script type="text/javascript">
<!--
  document.write(last_element);
  document.write(number_rows);
// -->
</script>

它根据需要显示URL(最后一个元素),但当我在页面之间浏览时,行数保持为1,而不是我希望达到的2、3、4等


有人能给我一些提示吗?

每次刷新页面时,JavaScript都会重新刷新。如果需要数据持久性,则需要使用cookie、
localStorage
或服务器端数据存储

所有这些选项都需要对字符串进行序列化和反序列化

下面是一个快速示例,说明如何使用
localStorage

//closure to prevent global pollution
//it's good to be green
(function () {
    "use strict";
    var history,
        url;
    //set url here
    //I'm making the assumption that no url will have a ',' char in it
    history = localStorage['history'].split(',');
    history.push(url);
    localStorage['history'] = history.join(',');
    console.log(url, history.length);
}());

在页面之间浏览时,会在每个页面上重新创建javascript环境。所以您总是从一个空数组开始

每次刷新页面时,所有javascript变量都将被删除。如果您需要持久性,您可以尝试html5本地存储:欢迎来到,请确保您花时间阅读。谢谢ZZBOV。。。回到绘图板上。谢谢肖恩。。。我想这是一种类似的东西。
//closure to prevent global pollution
//it's good to be green
(function () {
    "use strict";
    var history,
        url;
    //set url here
    //I'm making the assumption that no url will have a ',' char in it
    history = localStorage['history'].split(',');
    history.push(url);
    localStorage['history'] = history.join(',');
    console.log(url, history.length);
}());