dongxun1142 2016-05-14 18:08
浏览 32
已采纳

重新加载页面时Javascript var更改

I have a string array in javascript. But when I use href='mypage.php?id=var', I lost this array. I need keep it out for use it in the $_GET. This is the code:

<script>

var element_selected=[];
var i = 0;

function hrefPage()
{
    var pagina = "index.php?id=renew";
    location.href = pagina;
}

function loadArray(value)
{
    element_selected[i] = value;
    i++;
}

</script>

<?php

if(isset($_GET['id']))
{
    if ($_GET['id'] == "renew")
    {
        $selected_elements = array();
        $j = 0;
        for($j = 0; $j < "<script> document.write(i) </script>"; $j++)
        {
            $selected_elements[j] = "<script> document.write(elements_selected[j]) </script>";
            echo $selected_elements[j];
        }
    }
}

?>
  • 写回答

1条回答 默认 最新

  • duancaoqin6683 2016-05-14 18:18
    关注

    You need to use local storage to use retain javascript variables value across page reloads

            var varName = "sachin";
            localStorage.setItem("varName", VarName);
    
            alert(localStorage.getItem("varName"));
    

    Another example how to store array in localstorage

    var arrayName= [];
    arrayName[0] = prompt("Enter your value");
    localStorage.setItem("arrayName", JSON.stringify(arrayName));
    
    //...
    var storedValues = JSON.parse(localStorage.getItem("arrayName"));
    

    I have added a simple example here how to set javascript value in localstorage and how to access it

    I have provided it as an information for you to retain value across page reloads but first thing about http is that it is a stateless protocol .Each request fetches data from server and renders it in browser the value here is set in local storage of browser .So if you want to reload some value to script after page reload you need to set some flags and on page load check that flag if flag condition for the required situation arises get data from localstorage else proceed as normal

    Check out this tutorial for more information

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记