简体   繁体   English

Javascript-将链接写到新标签页

[英]Javascript - write links to new tabs

Using Javascript in a firefox extension, I have opened a new tab. 在firefox扩展中使用Javascript,我打开了一个新标签。 I am unaware of how I can write a link to www.google.com and other links (a whole list) in this tab, where the user can click a link and this page will open. 我不知道如何在该标签中编写指向www.google.com的链接以及其他链接(整个列表),用户可以在其中单击链接,然后将打开此页面。

Thank you for your help 谢谢您的帮助

so far I had typed in : 到目前为止,我已经输入:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());

Unfortunately this won't work: 不幸的是,这行不通:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newdocument=newTabBrowser2.contentDocument.documentElement.textContent;
newdocument.write("<a href=\"http://www.google.com\">google</a><br>");
newdocument.write("<a href=\"http://www.yahoo.com\">yahoo</a><br>");

and I've tried this: 我已经试过了:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newTabBrowser2.contentDocument.documentElement.innerHTML += "<a 

href=\\" http://www.google.com \\">google href = \\“ http://www.google.com \\”> google
"; “;

but that only works when I use the debugger 但这仅在我使用调试器时有效

Any idea why? 知道为什么吗?

Thanks 谢谢

It's not very clear from your question what you want. 从您的问题中不清楚您想要什么。 Maybe something like: 也许像这样:

newwindow=window.open();
newdocument=newwindow.document;
newdocument.write("<a href=\"http://www.google.com\">google</a><br>");
newdocument.write("<a href=\"http://www.yahoo.com\">yahoo</a><br>");
newdocument.close();

??? ???

I don't believe you can use textContent to add HTML content to a document - you're possibly better off using the DOM to construct the HTML. 我不相信您可以使用textContent将HTML内容添加到文档中-您最好使用DOM来构造HTML。

How about something like this (untested): 这样的东西(未测试)如何:

var newTabBrowser2 = gBrowser.getBrowserForTab(gBrowser.selectedTab = gBrowser.addTab());
newdocument=newTabBrowser2.contentDocument.documentElement;

var link=newdocument.createElement("a");
link.setAttribute("href", "http://www.google.com");
link.textContent="google";
newdocument.appendChild(link);

newdocument.appendChild(newdocument.createElement("br"));

link=newdocument.createElement("a");
link.setAttribute("href", "http://www.yahoo.com");
link.textContent="yahoo";
newdocument.appendChild(link);

newdocument.appendChild(newdocument.createElement("br"));

Alternatively, it may be possible to just write to the innerHtml of the document element. 或者, 可以只写到文档元素的innerHtml。

This looks like the sort of thing you're looking for. 这看起来像您要找的东西。

http://mesh.typepad.com/blog/2004/11/creating_a_new_.html http://mesh.typepad.com/blog/2004/11/creating_a_new_.html

var myUrl = "http://mesh.typepad.com";
var tBrowser = document.getElementById("content");
var tab = tBrowser.addTab(myUrl);

This creates a new tab every time it's run - you can update the url of a pre-existing tab like this: 每次运行时,都会创建一个新标签页-您可以像这样更新现有标签页的网址:

var uri = "http://mesh.typepad.com";
tBrowser.getBrowserForTab(tab).loadURI(uri);

Finally, you should be able to set the focus to the new tab: 最后,您应该能够将焦点设置到新选项卡上:

tBrowser.selectedTab = tab;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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