简体   繁体   中英

Switch between tabs without reload and restrict opening new tab with same url

I have a scenario where I have 2 html pages. On both html page, I have other html page's link. So my requirements are: 1. When I open first HTML page and click on link to HTML2, new tab in same window should be opened. And HTML 2 page has link for HTML1. So when I click on HTML1. HTML page already opened in tab1 should be focused. 2. While switching between tabs, page should not be reloaded. 3. Now assume I have 2 tabs open in window(HTML1 and HTML2). When i try to manually copy and paste HTML1 url in tab3, it should be focused on Tab1.

I have used following code to achieve point 1 and 2 but struggling to achieve point 3. Any leads will be helpful.

 //HTML1

<html>
<head>
<title>HTML1</title>
<script type="text/javascript">
window.onload = function (){
window.name = "HTML1"
}   
var windowObjectReference = null; // global variable
function openHTML2App() {
if(windowObjectReference == null || windowObjectReference.closed)
{
    windowObjectReference = window.open("file:///D:/HTML2.html","HTML2");
    console.log("in if");
}
else
{
    windowObjectReference.focus();
    console.log("In else");
};
}
</script>
</head>
<body>
<h1>This is HTML1</h1>
<a target="HTML2" onclick="openHTML2App(); return false;" >This is link for HTML2</a>
</body>
</html>

//HTML2

<html>
<head>
<title>HTML2</title>
<script type="text/javascript">
window.onload = function (){
window.name = "HTML2"
}
var windowObjectReference = null; // global variable
function openHTML1App() {
if(windowObjectReference == null || windowObjectReference.closed)
{
    windowObjectReference = window.open("","HTML1");
}
else
{
    windowObjectReference.focus();
};
}
</script>
</head>
<body>
<h1>This is HTML2</h1>
<a href = "file:///D:/HTML1.html" target="HTML1" onclick="openHTML1App(); return false;" >This is link for HTML1</a>
</body>
</html>

Actual Result: Page is reloaded everytime when tabs are switched. And when manually you enter URL in new tab, new tab with URL is opened. Expected result is: 1. On tab switching page should not be reloaded. 2. restrict opening URL if already opened rather focus on already opend url

出于安全原因,您不能强迫用户导航到另一个选项卡。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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