views:

738

answers:

3

Hi,

Using Javascript in a firefox extension, I have opened a new tab. 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.

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\"&gt;google&lt;/a&gt;&lt;br&gt;");
newdocument.write("<a href=\"http://www.yahoo.com\"&gt;yahoo&lt;/a&gt;&lt;br&gt;");

and I've tried this:

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

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

but that only works when I use the debugger

Any idea why?

Thanks

A: 

This looks like the sort of thing you're looking for.

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;
Cyphus
thanks for your replyI am more concerned with displaying a whole list of links onto a tab and the user chooses one of these
Lily
+1  A: 

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\"&gt;google&lt;/a&gt;&lt;br&gt;");
newdocument.write("<a href=\"http://www.yahoo.com\"&gt;yahoo&lt;/a&gt;&lt;br&gt;");
newdocument.close();

???

danio
+1  A: 

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.

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.

Cyphus