社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Jquery

Jquery刷新页面,然后执行一些操作

phingoc • 5 年前 • 1982 次点击  

页面刷新后是否可以使用Jquery或JavaScript执行命令? 像这样的东西,exept,我的代码不起作用。

<script>
$('#some-button').click(function() {
   location.reload();
   $('#another-button').trigger("click");
});
</script>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/53222
 
1982 次点击  
文章 [ 4 ]  |  最新文章 5 年前
AdeelApple
Reply   •   1 楼
AdeelApple    5 年前

您可以使用localstorage技巧来实现这一点。

 <script>
    $('#some-button').click(function() {
       // Save on local storage
       localStorage.setItem("myfunction", "dosomething");
       location.reload();

    });

    $(document).ready(function(){
        // Retrieve stored data
        if(localStorage.getItem("myfunction")=="dosomething")
        {
           //remove saved data
           localStorage.removeItem("myfunction");
           //do your stuff here
           $('#another-button').trigger("click");
         }
     });
    </script>

如果你想做类似的事情,你也可以使用服务器端会话来存储数据,以便在不同的页面之间共享。 使用AJAX这是最好的方法,你可以在javascript或jquery中轻松做到这一点。

$.post('pageAdress',{'key':'value'},function(result){
    //do youe stuff here
});

  1. 要调用的页地址
  2. 如果需要,可以通过post方法传递数据
  3. 回调函数

这可以在不重新加载页面的情况下执行代码

mad max
Reply   •   2 楼
mad max    5 年前

checkReloadedPage(){
      if (window.performance) {
        console.info("window.performance working");
      }
      if (performance.navigation.type == 1) {
       // mean page is reloaded
        return 1;
      } else {
        //mean page is not reloaded
        return 2;
      }
    }

导航计时api: https://www.w3.org/TR/navigation-timing/

Pranav C Balan
Reply   •   3 楼
Pranav C Balan    5 年前

您可以在刷新页面之前在本地存储中保留一个条目,并在页面加载时检查是否在本地存储中存在密钥,从而完成一个简单的解决方法。

<script>
$('#some-button').click(function() {
   localStorage.setItem('_execute_after_refresh', 'some_value')
   location.reload();
});


if(localStorage.getItem('_execute_after_refresh') !== null){
   $('#another-button').trigger("click");
   localStorage.removeItem('_execute_after_refresh')
}

</script>
prasanth
Reply   •   4 楼
prasanth    5 年前

对。可能的

haschange

  1. 更新url的一些散列值 onClick
  2. 然后匹配 hasvalue

代码

<script>
window.addEventListener("hashchange", hasChanger, false);

function hasChanger(){
  var key = window.location.hash
  if(key == '#trigger'){
     $('#another-button').trigger("click");
  }
}

$('#some-button').click(function() {
   window.location.href = window.location.href+'#trigger'
});
</script>