javascript 如何使用javascript检查页面是否第一次加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31605570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to check if page is loaded for the first time using javascript
提问by user123456789
I want to check if the page is being loaded for the first time, and if it is then display the filter. If I put showFiltermenu() in the pageLoad function then it will show every time the page is loaded but I just want it display the first time. I tried using Page.IsPostBack
but that doesn't display the filter.
我想检查页面是否是第一次加载,如果是,则显示过滤器。如果我将 showFiltermenu() 放在 pageLoad 函数中,那么它会在每次加载页面时显示,但我只希望它第一次显示。我尝试使用Page.IsPostBack
但不显示过滤器。
<script type="text/javascript">
function showFiltermenu() {
$("#filtermenuDrop").toggle('fold', {}, 500);
}
function closefiltermenu() {
$("#filtermenuDrop").toggle('fold', {}, 500);
}
function pageLoad() {
$("input[rel^='datepicker']").datepicker({
dateFormat: 'dd/mm/yy',
changeMonth: true,
yearRange: "c-50:c+50",
changeYear: true,
showOn: "both",
firstDay: 1,
buttonImage: "../images/icons/buttons/basic1-049-small.png"
});
<% if (Page.IsPostBack)
{ %>
showFiltermenu();
<% } %>
ShadowboxInit();
}
采纳答案by user123456789
Thanks everyone for their answers but there was just a simple mistake in my question. I forgot the !
in the if statement. This now works to check if the page is loaded for the first time.
感谢大家的回答,但我的问题只是一个简单的错误。我忘记了!
if 语句中的。现在可以检查页面是否是第一次加载。
<% if (!Page.IsPostBack)
{ %>
showFiltermenu();
<% } %>
回答by Santiago Hernández
with localStorage
you can save values like this:
与localStorage
您可以节省这样的价值观:
var firstTime = localStorage.getItem("first_time");
if(!firstTime) {
// first time loaded!
localStorage.setItem("first_time","1");
}
no jQuery, no plugins, just pure, beautiful and fast HTML5 API
没有 jQuery,没有插件,只有纯粹、美观和快速的 HTML5 API
回答by Giovanni Giordano
Try with cookie. The cookie are very usefull for this kind of thing. First of all let's see if the browser know cookie:
用饼干试试。cookie 对于这种事情非常有用。首先让我们看看浏览器是否知道cookie:
if(document.cookie)
{document.cookie="Name=Value";
}
else{
alert("perhaps you must change browser.Something in this page can't load.")
}
Remember,cookie return always a string,if you write down on the console
记住,cookie 总是返回一个字符串,如果你在控制台上写下来
document.cookie
Set a cookie is simple.You can set a lot of cookie.But if you delete the cookies,the browser things that you are visiting the website for the first time. To check if a cookie is set you can do this:
设置cookie很简单。你可以设置很多cookie。但是如果你删除cookie,浏览器就是你第一次访问网站的东西。要检查是否设置了 cookie,您可以执行以下操作:
if(document.cookie.search="value")
{//code that you want to execute if the page is visited yet}