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 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line