<small id='DKla8'></small><noframes id='DKla8'>

    <i id='DKla8'><tr id='DKla8'><dt id='DKla8'><q id='DKla8'><span id='DKla8'><b id='DKla8'><form id='DKla8'><ins id='DKla8'></ins><ul id='DKla8'></ul><sub id='DKla8'></sub></form><legend id='DKla8'></legend><bdo id='DKla8'><pre id='DKla8'><center id='DKla8'></center></pre></bdo></b><th id='DKla8'></th></span></q></dt></tr></i><div id='DKla8'><tfoot id='DKla8'></tfoot><dl id='DKla8'><fieldset id='DKla8'></fieldset></dl></div>
  1. <legend id='DKla8'><style id='DKla8'><dir id='DKla8'><q id='DKla8'></q></dir></style></legend>

    • <bdo id='DKla8'></bdo><ul id='DKla8'></ul>

    <tfoot id='DKla8'></tfoot>

      打开标签的数量限制

      时间:2023-06-14

      <tfoot id='NpI3C'></tfoot>
        <tbody id='NpI3C'></tbody>

        <i id='NpI3C'><tr id='NpI3C'><dt id='NpI3C'><q id='NpI3C'><span id='NpI3C'><b id='NpI3C'><form id='NpI3C'><ins id='NpI3C'></ins><ul id='NpI3C'></ul><sub id='NpI3C'></sub></form><legend id='NpI3C'></legend><bdo id='NpI3C'><pre id='NpI3C'><center id='NpI3C'></center></pre></bdo></b><th id='NpI3C'></th></span></q></dt></tr></i><div id='NpI3C'><tfoot id='NpI3C'></tfoot><dl id='NpI3C'><fieldset id='NpI3C'></fieldset></dl></div>

          <bdo id='NpI3C'></bdo><ul id='NpI3C'></ul>

            1. <small id='NpI3C'></small><noframes id='NpI3C'>

              <legend id='NpI3C'><style id='NpI3C'><dir id='NpI3C'><q id='NpI3C'></q></dir></style></legend>
                本文介绍了打开标签的数量限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                网页上有一些链接.

                右键单击有在新选项卡中打开链接"选项(浏览器选项).

                On right click there is option of 'open link in new tab'(browser option).

                我想限制用户不要打开两个以上的标签?我该怎么做?

                I want to restrict user for not opening more that two tabs? How can i do this?

                <%@ page language="java" contentType="text/html; charset=UTF-8"
                pageEncoding="UTF-8"%>
                <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                <html>
                <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                <title>Insert title here</title>
                </head>
                <body>
                <ul>
                <li><a href="http://localhost:8080/struts_tab/abcForm1.action" oncontextmenu="return false;"><span>First Click[Right Click disabled]</span></a></li>
                <li><a href="http://localhost:8080/struts_tab/defForm2.action"><span>Second clieck[Not more than 2 tabs]</span></a></li>
                </ul>
                </body>
                </html>
                

                推荐答案

                您不能限制用户打开新标签页.
                (这让我想起了没有按钮,没有地址栏,但仍然响应退格和其他事件的旧弹出窗口)

                You can't restrict the user from opening a new tab.
                (This reminds me the old pop-ups with no buttons, no address bar, but still responding to backspace and other events)

                但是,您可以让您的应用识别打开第三个标签页的尝试,并加载不同的结果(例如错误消息),例如:

                You can however make your app recognize the attempt of opening a third tab, and load a different result like an error message, for example:

                已达到最大打开标签限制.请同时使用不超过两个选项卡.关闭此标签

                Maximum open tabs limit reached. Please use no more than two tabs concurrently. close this tab

                为此,您可以使用 HTML5 sessionStorage.
                注意:Web 存储(sessionStoragelocalStorage)现在所有浏览器都支持.

                To do this, you can use HTML5 sessionStorage.
                Note: Web Storage (sessionStorage and localStorage) is supported on every browser nowadays.

                这是一个全局对象(sessionStorage),维护一个存储区域在页面会话期间可用.页面会话只要浏览器打开并在页面上存活,就会持续重新加载和恢复.在新标签页或窗口中打开页面会导致将启动一个新会话.

                sessionStorage

                This is a global object (sessionStorage) that maintains a storage area that's available for the duration of the page session. A page session lasts for as long as the browser is open and survives over page reloads and restores. Opening a page in a new tab or window will cause a new session to be initiated.

                那你就可以了

                • 如果sessionStorage中不存在,在JSP中生成一个唯一的token,放到sessionStorage中,

                • if not present in sessionStorage, generate an unique token in JSP, and put it in sessionStorage,

                $(function(){
                    // Read the ID. If it's null, this is a new tab: 
                    // generate the ID and store it for later.
                    var tabId = sessionStorage.getItem("tabId");
                    if (tabId == null){
                        tabId = Math.random();
                        sessionStorage.putItem("tabId",tabId);
                    }
                

              • 将其发送回操作

              • send it back to the action

                    // Add the ID to the form (as hidden field), 
                    // so it will be posted back in next submission.
                    $('<input>').attr('type'  , 'hidden')
                                .attr('name'  , 'tabId')
                                .attr('value' , tabId)
                    .appendTo('form');
                });
                

                ,可能是 BaseAction 中的 setter,由其他操作扩展,并由 prepare() 读取,或者 在拦截器中更好

                , maybe to a setter in a BaseAction, extendend by the other actions, and read by prepare(), or much better in an Interceptor;

                把它放在一个集合中,检查它是否已经包含两个元素,否则返回错误结果,应该全局映射:

                put it in a collection checking that it doesn't contain already two elements, otherwise return the error result, that should be mapped globally:

                public String intercept(ActionInvocation actionInvocation) throws Exception {
                    Action action = (Action) actionInvocation.getAction();
                    if(action instanceof LimitedTabsAware){ //interface to identify special actions
                        ActionContext context = actionInvocation.getInvocationContext();
                        Map<String, String[]> request = ((HttpServletRequest) 
                                            context.get(StrutsStatics.HTTP_REQUEST)).getParameterMap();
                
                        if (request.containsKey("tabId")){              
                            String tabId = (String) request.get("tabId")[0];
                            List<String> openTabs = context.getSession().get("OPEN_TABS_KEY");
                
                            if (openTabs.contains(tabId)){
                                return actionInvocation.invoke();                   
                            } else if (openTabs.size()>=2){
                                return "tabLimitExceeded"; // global result
                            } else {
                                openTabs.add(tabId);
                                context.getSession().put("OPEN_TABS_KEY", openTabs);
                                return actionInvocation.invoke();
                            }
                
                        } else {
                            throw new IllegalArgumentException("There is no tabId in this request.");
                        }
                    } else {
                        return actionInvocation.invoke();
                    }
                }
                

              • 然后您应该找到一种方法来识别选项卡何时关闭(以释放一个插槽),方法是:

                Then you should find a way to recognize when a tab get closed (to free one slot), by either:

                • 优化集合中元素的有效期(如果您有一段时间不使用标签,会话过期,所以必须做集合中的令牌)
                • 否则,在您的页面中放置一个 javascript AJAX 计时器(例如,每 30 秒),发送一个 keep-alive 向动作发出信号以刷新元素的有效性.如果选项卡关闭,则不再发送信号.
                • timizing the period of validity of the elements in your collection (if you don't use a tab for some time, the session expires, and so must do the token in the collection)
                • otherwise, putting a javascript AJAX timer in your page (eg. every 30 seconds), that send a keep-alive signal to an action to refresh the validity of the element. If the tab get closed, the signal is not sent anymore.

                这篇关于打开标签的数量限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何使用 jquery 验证插件验证名称中带有点的输入字段? 下一篇:如何在 Struts2 中获取特定的未选中复选框

                相关文章

                1. <small id='2Y3qP'></small><noframes id='2Y3qP'>

                    <bdo id='2Y3qP'></bdo><ul id='2Y3qP'></ul>
                  <legend id='2Y3qP'><style id='2Y3qP'><dir id='2Y3qP'><q id='2Y3qP'></q></dir></style></legend>

                  <i id='2Y3qP'><tr id='2Y3qP'><dt id='2Y3qP'><q id='2Y3qP'><span id='2Y3qP'><b id='2Y3qP'><form id='2Y3qP'><ins id='2Y3qP'></ins><ul id='2Y3qP'></ul><sub id='2Y3qP'></sub></form><legend id='2Y3qP'></legend><bdo id='2Y3qP'><pre id='2Y3qP'><center id='2Y3qP'></center></pre></bdo></b><th id='2Y3qP'></th></span></q></dt></tr></i><div id='2Y3qP'><tfoot id='2Y3qP'></tfoot><dl id='2Y3qP'><fieldset id='2Y3qP'></fieldset></dl></div>

                  1. <tfoot id='2Y3qP'></tfoot>