使用jQuery检测首页加载? [英] Detect first page load with jQuery?
问题描述
我需要检测第一次在jQuery中加载页面,以便只有在用户第一次导航到该页面时加载页面时才能执行某些操作。类似于服务器端代码页.ispostbasck。我已经测试了$(document).ready并且每次页面加载时它都会触发,所以这不会提供我需要的东西。我也尝试过jQuery Load函数 - 它还会触发每个页面加载。所以通过页面加载一个例子是我在类型按钮的页面上有一个HTML输入标签,它不会触发回发(如asp.net按钮),但它会重新加载页面并触发$(文档).ready
I need to detect the first time a page loads in jQuery so that I can perform some actions only when the page loads the first time a user navigates to that page. Similar to server side code page.ispostbasck. I have tested $(document).ready and it fires every time the page loads so this will not provide what I need. I have also tried the jQuery Load function - it also fires every page load. So by page load an example is that I have an HTML input tag on the page of type button and it does not fire a postback (like an asp.net button) but it does reload the page and fires $(document).ready
谢谢
推荐答案
您必须使用cookie存储首次加载信息:
You will have to use cookie to store first load information:
if (! $.cookie("cookieName")){
// do your stuff
// set cookie now
$.cookie("cookieName", "firstSet", {"expires" : 7})
}
注意:以上示例使用 jQuery Cookie插件。
这篇关于使用jQuery检测首页加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!