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

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

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历