简体   繁体   English

页面刷新后如何在我的 Javascript 页面中获取价值

[英]How can I get value in my Javascript page after page refresh

<a href="<?php echo base_url(); ?>userprofile/userprofile/2"  style="color:white; margin-top:2rem;" onclick="senduser(2);">User2</a>

after clicking "User2", page refreshing.点击“User2”后,页面刷新。 But in order to get images I need to send a Id value to my app.js file.但是为了获取图像,我需要向我的 app.js 文件发送一个 Id 值。 I've tried to provide this using onclik function.我尝试使用 onclik 功能提供此功能。 Unfortunately!很遗憾! after page refresh it could not!页面刷新后就不行了!

function senduser($id){

    var limit_load = 5;
    var start_load = 0;
    var userId = $id;
    function load_photo_profile(limit_load, start_load, userId) {

        var url = baseUrl + "userprofile/loadmore";

        $.post(url, { limit: limit_load, start: start_load, userId: userId }, function (response) {


            $(".included_image").append(response);
            $('#mygallery').justifiedGallery({
                rowHeight: 285,
                lastRow: 'nojustify',
                margins: 10
            });


        });

    };

    load_photo_profile(limit_load, start_load, userId);

    $(window).scroll(function () {
        if ($(window).scrollTop() >= $(document).height() - $(window).height()) {
            start_load = start_load + limit_load;
            load_photo_profile(limit_load, start_load, userId);
        }
    });



}

This id triggers to work this function complately.这个 id 触发完全地工作这个功能。 following it triggers a ajax post method to render code inside mygallery element.跟随它触发 ajax post 方法以在 mygallery 元素内呈现代码。 on the other hand I have to refreh my page .另一方面,我必须刷新我的页面。

You could use sessionStorage to save a value you need, so after refresh the page, you can access to that value, you can create it like this:您可以使用sessionStorage来保存您需要的值,因此在刷新页面后,您可以访问该值,您可以像这样创建它:

sessionStorage.setItem('nameOfItem', "mysavedValue");

And for access the sessionStorage value this:对于访问sessionStorage值:

sessionStorage.getItem('nameOfItem')

localstorage 本地存储

Session storage is temporary which exists as long as the current window is opened and data gets lost when window is closed where as Local Storage is persistant and remains intact across sessions unless anyone deletes it.会话存储是临时的,只要当前窗口打开,数据就会在窗口关闭时丢失,而本地存储是持久的,并且在会话之间保持完整,除非有人删除它。


localStorage.setItem("nameOfItem","valueofitem");//set

localStorage.getItem("nameOfItem"); //get




声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM