简体   繁体   English

如果用户未登录,则发送警报消息

[英]Sending alert message if user not logged in

What would be the best way to send an alert message to the user if it requires them to login, but at the same time avoid an ajax call? 如果要求用户登录,但同时避免ajax调用,向用户发送警报消息的最佳方法是什么? I might have code like <a href="#" id="vote"> which would trigger an ajax function based on the click. 我可能有类似<a href="#" id="vote">代码,该代码会基于点击触发ajax函数。 I was thinking I would check on the server side if a user is logged in and replace it with: <a href="#" class="not-logged-in"> . 我以为我会在服务器端检查是否已登录用户,并将其替换为: <a href="#" class="not-logged-in"> If that is clicked then it will alert them that they need to log in. The problem is that I might have some css styles for the #vote. 如果单击该按钮,它将提醒他们他们需要登录。问题是我可能对#vote有某些CSS样式。 If instead I make it <a href="#" id="vote" class="not-logged-in"> it would trigger both the ajax call and alert, so I'd have to wrap the ajax call around with a check to see that the not-loggin-in class does not exist, but it seems a bit tedious to do that for all my click functions that have ajax calls. 相反,如果我将其设置为<a href="#" id="vote" class="not-logged-in"> ,它将同时触发ajax调用和警报,因此我必须用a来包装ajax调用检查以查看not-loggin-in类是否不存在,但是对我所有具有ajax调用的click函数执行此操作似乎有些乏味。 Or either I would call the parent id and set the style like #parentId a{}? 还是我叫父母ID并将样式设置为#parentId a {}? Or is there any better methods? 还是有更好的方法? I'm not sure what people usually do, so looking for some opinions. 我不确定人们通常会做什么,因此需要一些意见。 Thanks! 谢谢!

How about this: 这个怎么样:

The HTML: HTML:

<body class="not-logged-in"> -- or -- <body class="logged-in">
<a href="/vote/13" class="vote">Vote!</a>

The jQuery: jQuery:

$('body.logged-in .vote').click(function(){
    $(this).append($.get($(this).attr('href'));
    return false;
});

If your click handler for the a#vote element is tied to the vote id, you'll have to either change the id for the logged out users or add a conditional to the click handler. 如果您将a#vote元素的点击处理程序与vote ID绑定在一起,则必须更改已注销用户的ID或向该点击处理程序添加条件。

You mentioned that you don't want to check for the not-logged-in class before proceeding with the AJAX call, so I'm assuming this means you'd rather leave the click handler alone and come up with some other way to have the link look like the vote link but not act like the vote link. 您提到您不想在继续进行AJAX调用之前检查not-logged-in类,所以我假设这意味着您宁愿不考虑单击处理程序,而想出其他方法该链接看起来像投票链接,但不像投票链接那样。

You could make an alternate id and give it the same styling, like so: 您可以制作一个备用ID并为其设置相同的样式,如下所示:

HTML: HTML:

<a href="#" id="fakeVote"> 

CSS: CSS:

#vote, #fakeVote {
  /* CSS here */
}

The nice thing about this is that you can just add a new click handler for the a#fakeVote element to alert the logged out users. 这样做的a#fakeVote是,您可以为a#fakeVote元素添加一个新的单击处理程序,以警告已注销的用户。

If you want to reduce the DOM lookup time with each click just attach the "loggedIn" class to body element and do the check right after page load. 如果您想通过每次单击来减少DOM查找时间,只需将“ loggedIn”类附加到body元素,然后在页面加载后立即进行检查。

var loggedIn = $('body').hasClass('loggedIn');

Now, you don't have to make a check on the link itself but rather on that variable. 现在,您不必检查链接本身,而是检查该变量。

Alternatively, write a wrapper for your ajax calls. 或者,为您的ajax调用编写包装器。

$.fn.ajaxLoggedIn = function() { 
  if (loggedIn) { 
    $.ajax.apply(this, arguments);
  } 
  else {
    alert('You are not logged in');
  }
}

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

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