There are some links on web-page.
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:
To do this, you can use HTML5 sessionStorage.
Note: Web Storage (
sessionStorage
andlocalStorage
) is supported on every browser nowadays.Then you can
if not present in sessionStorage, generate an unique token in JSP, and put it in sessionStorage,
send it back to the action
, 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:
Then you should find a way to recognize when a tab get closed (to free one slot), by either:
keep-alive
signal to an action to refresh the validity of the element. If the tab get closed, the signal is not sent anymore.