javascript - 如何从原始选项卡获取新打开的浏览器选项卡名称?

标签 javascript jquery html

我的代码是这样的,用于在同一浏览器中创建从一个选项卡到另一个选项卡的新选项卡。

function newTab()
{
    var form1 = document.createElement("form");
    form1.id = "newTab1"
    form1.method = "GET";
    form1.action = "domainname";  //My site address
    form1.target = "framename";   //Browser tab name
    document.body.appendChild(form1);
    form1.submit();
}

以上代码可以正确创建新选项卡。当单击“MyNewTab”链接主页时

<a href="javascript:newTab()" style=" margin-left:15px;" id="newTab"> MyNewTab </a>

我再次尝试将主页切换到新打开的选项卡。这次需要从主页切换到新标签页而不需要重新加载。但是这个新标签内容重新加载了。

如何通过单击“MyNewTab”链接获取选项卡名称并聚焦新打开的选项卡?

最佳答案

你需要在javascript cookie中设置当前选中的标签,并且总是在页面加载时读取这个cookie并设置标签

您可以在 newTab() 函数中设置 cookie:

function newTab()
{
    var form1 = document.createElement("form");
    form1.id = "newTab1"
    form1.method = "GET";
    form1.action = "domainname";  //My site address
    form1.target = "framename";   //Browser tab name

    $.cookie("currentTab", "framename");//set the selected tab name in cookie

    document.body.appendChild(form1);
    form1.submit();
}

然后,在页面加载事件中:

var tabName=$.cookie("currentTab")//get the current tab name from cookie.
if(tabName==null){
tabName="default tab name"//at first time the cookie will be null, 
//so you need to assign a default value if the cookie is null
}

//set this tab as selected

关于javascript - 如何从原始选项卡获取新打开的浏览器选项卡名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34545983/

相关文章:

javascript - Node Js Firestore 使用空格访问数据库值

javascript - jQuery 从浏览器检测最大化和恢复按钮

javascript - 将日期中的 JavaScript 三元运算转换为条件语句

javascript - Jquery position() 包括 margin

jquery - 使用 JQuery 将相同的计算应用于不同的输入

javascript - 从数据库中将 Iframe 注入(inject) Angular 2

javascript - 将 anchor 标记转换为嵌入 jquery

javascript - JQuery:复选框链无法正常工作

html - 基于条件的 CSS 选择器

html - 如何在 Vue 组件中将一个图标覆盖在另一个图标之上?