Once the user is on my page, I do not want him to refresh the page.

  1. Anytime, the user hits F5 or refresh button on top. He should get an alert saying

    您不能刷新页面.

  2. 此外,如果用户打开一个新选项卡并try 访问前一个选项卡中的相同URL,他应该会收到警告

    You cannot open same page in 2 tabs

Anyway I can do this using JavaScript or jQuery? Point one is really important.

推荐答案

#1 can be implemented via window.onbeforeunload.

For example:

<script type="text/javascript">
    window.onbeforeunload = function() {
        return "Dude, are you sure you want to leave? Think of the kittens!";
    }
</script>

The user will be prompted with the message, and given an option to stay on the page or continue on their way. This is becoming more common. Stack Overflow does this if you try to navigate away from a page while you are typing a post. You can't completely stop the user from reloading, but you can make it sound real scary if they do.

#2 is more or less impossible. Even if you tracked sessions and user logins, you still wouldn't be able to guarantee that you were detecting a second tab correctly. For example, maybe I have one window open, then close it. Now I open a new window. You would likely detect that as a second tab, even though I already closed the first one. Now your user can't access the first window because they closed it, and they can't access the second window because you're denying them.

事实上,我的银行的在线系统真的很努力地做第二件事,上面描述的情况一直都在发生.我通常必须等到服务器端会话到期后才能再次使用银行系统.

Jquery相关问答推荐

使用jquery ui来回滑动切换

Bootstrap 3模态框和video.js视频未正确调整大小

Jquery如何通过数组中的属性查找对象

确定元素是否是其父元素的最后一个子元素

在 jQuery 中构建 html 元素的最清晰方法

使用 jQuery 为 box-shadow 设置动画的正确方法

了解 Backbone.js REST 调用

jQuery .hide() 和 .css("display", "none") 的区别

如何判断元素是否在屏幕外

jquery在加载时获取iframe内容的高度

覆盖 twitter bootstrap Textbox Glow and Shadows

jQuery Uncaught TypeError: 对象 [object Window] 的属性$不是函数

如何使用 javascript (jquery) 将整数值添加到返回字符串的值中?

jquery分别绑定双击和单击

通过 :not 在 jQuery Select 器中隐藏除 $(this) 之外的所有内容

在 jQuery 中 Select 后代元素的最快方法是什么?

在事件中使用 jQuery 获取被点击的元素?

Jquery 日期 Select 器 z-index 问题

jquery在cookie中保存json数据对象

使用 JQuery 判断组中是否没有选中单选按钮