Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/405.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何设置新浏览器选项卡的标题?_Javascript_Html_Css - Fatal编程技术网

Javascript 如何设置新浏览器选项卡的标题?

Javascript 如何设置新浏览器选项卡的标题?,javascript,html,css,Javascript,Html,Css,我对链接的新选项卡有一个问题 在用户单击链接之前,是否可以设置浏览器选项卡标题?如果新选项卡中包含的html没有title属性,那么似乎无法讨论新选项卡的标题。我说得对吗?如何设置标题 //the href is dynamic so I can't set them one by one because I have 100+ html file here <a href="test.html" target="_blank">open me<a> //href是动态

我对链接的新选项卡有一个问题

在用户单击链接之前,是否可以设置浏览器选项卡标题?如果新选项卡中包含的html没有title属性,那么似乎无法讨论新选项卡的标题。我说得对吗?如何设置标题

//the href is dynamic so I can't set them one by one because I have 100+ html file here
<a href="test.html" target="_blank">open me<a>
//href是动态的,所以我无法逐个设置它们,因为我这里有100多个html文件
打开我

如果您在第1页,并在新选项卡中打开第2页,则无法从第1页设置第2页的标题


如果您有权访问第2页,则有可能,否则就没有。

您可以通过哈希传递标题,并在另一页上获取它,如果这另一页是您的,您可以修改其代码

第1页:

...
<a href="test.html#the_title_you_want" target="_blank">open me<a>
...

您可以创建自己的第2页,在框架集中打开其他页面(无法编辑的页面)。然后,您可以在加载第2页时动态更改标题,或者在使用window.open时按照其他人的建议更改标题。您可以从父页控制标题。

正如您所拥有的,这是不可能的,因为您的链接只是普通的HTML链接。当新页面在新选项卡中打开时,当前页面将没有任何引用,因此无法以任何方式更改它。您需要使用javascript打开页面,并以此方式设置标题

您可以在
window onload
中动态设置此项,以查找所有
a
标记并添加
单击事件,然后打开窗口并设置标题

如果希望每个页面有不同的标题,可以将其存储在
a
标记中的
data-
属性中

请注意,这将仅适用于同一域中的页面(出于安全考虑),并且不会处理用户右键单击并按“在新窗口中打开”的操作。不过,在Windows中单击鼠标中键似乎确实有效

HTML

<a href="test.html" data-title="A new page" target="_blank">open me</a>

JavaScript

window.addEventListener("load", function() {

    // does the actual opening
    function openWindow(event) {
        event = event || window.event;

        // find the url and title to set
        var href = this.getAttribute("href");
        var newTitle = this.getAttribute("data-title");
        // or if you work the title out some other way...
        // var newTitle = "Some constant string";

        // open the window
        var newWin = window.open(href, "_blank");

        // add a load listener to the window so that the title gets changed on page load
        newWin.addEventListener("load", function() {
            newWin.document.title = newTitle;
        });

        // stop the default `a` link or you will get 2 new windows!
        event.returnValue =  false;
    }

    // find all a tags opening in a new window
    var links = document.querySelectorAll("a[target=_blank][data-title]");
    // or this if you don't want to store custom titles with each link
    //var links = document.querySelectorAll("a[target=_blank]");

    // add a click event for each so we can do our own thing
    for(var i = 0; i < links.length; i++) {
        links[i].addEventListener("click", openWindow.bind(links[i]));
    }

});
window.addEventListener(“加载”,函数(){
//实际的开口是多少
函数openWindow(事件){
event=event | | window.event;
//查找要设置的url和标题
var href=this.getAttribute(“href”);
var newTitle=this.getAttribute(“数据标题”);
//或者如果你以其他方式解决标题。。。
//var newTitle=“某些常量字符串”;
//打开窗户
var newWin=window.open(href,“_blank”);
//向窗口添加加载侦听器,以便在加载页面时更改标题
addEventListener(“加载”,函数(){
newWin.document.title=newTitle;
});
//停止默认的'a'链接,否则您将获得两个新窗口!
event.returnValue=false;
}
//查找在新窗口中打开的所有a标记
var links=document.queryselectoral(“a[target=\u blank][data title]”);
//如果您不想在每个链接中存储自定义标题,也可以使用此选项
//var links=document.queryselectoral(“a[target=\u blank]”);
//为每个添加一个单击事件,以便我们可以做自己的事情
对于(变量i=0;i

您有两个选择。使用纯HTML,您可以让用户打开链接,然后稍后更改标题。或者可以使用内联JavaScript更改标题。以下是您如何做到这两个方面:

方法1 通过指定目标属性更改链接,然后稍后使用该窗口名称控制文档。例如,在您的链接中,它将是:
。每当您想要更改此页面的标题时,都可以使用JavaScript:
window.open(“,”thenewindow“).document.title=“新建页面标题!”
方法2
通过指定onclick属性来更改链接,该属性将手动打开链接并立即更改页面标题。基本上,它看起来像:
。这将基于href属性打开窗口,立即更改标题,并将窗口设置为在加载完成时将标题更改为该属性(以防确实存在标题标记)



这两种方法(如其他人所述)的问题是html文件必须位于同一个域。

我没有看到addEventListener可靠地工作,尤其是在使用javascript打开新页面时。更改选项卡标题并使其可靠工作的最佳方法是设置页面加载前的超时。您可能需要使用超时值,但它可以工作

var newWindow = window.open(url, '_blank');

setTimeout(function () {
    newWindow.document.title = "My Tab Name";
}, 100);

最简单的方法如下:

var winTab = window.open("", "_blank")

//Open URL by writing iframe with given URL

winTab.document.write("write iframe with your url in src here")

//Set Title for the new tab

winTab.document.title = "Form Title"

你的意思是
document.title='foo'
href
是动态的,因此您必须从数据库或其他地方获得这些页面的一些信息。因此,在页面加载时将它们设置在那里…如何拥有新的选项卡页面?你还是一个外部链接?因为如果你可以将url上的标题设置为参数,并在选项卡打开时使用它,你的意思是你想用链接传递下一页的标题?@PaulS。我不能这样做,因为它会打开一个新的选项卡,我可以打开超过100个html文件。我没有访问这些html文件的权限,它们也没有在html中指定标题。通常,您可以在使用JS而不是链接打开选项卡时设置标题。在这种情况下,域当然必须相同。@Teemu如何?假设它们在同一个域上。如何在不触摸第2页的情况下更改第2页的标题?使用打开选项卡,这样您将看到选项卡的
窗口
以及其中的所有内容。谢谢Nicael,我想我误解了您的问题。他们在同一个域中,但我没有访问第二页的权限+虽然我尝试了最后一个选择——不为我工作——这主意不错,但却遭到了拒绝
var newWindow = window.open(url, '_blank');

setTimeout(function () {
    newWindow.document.title = "My Tab Name";
}, 100);
var winTab = window.open("", "_blank")

//Open URL by writing iframe with given URL

winTab.document.write("write iframe with your url in src here")

//Set Title for the new tab

winTab.document.title = "Form Title"