简体   繁体   中英

Redirect Current Tab From Firefox Extension

How can I redirect the current tab when a button in a panel is clicked?

What I currently have in my code right now is the following :

Javascript

function doSearch (){
  alert("Search!"); 
  var tabs = require("tabs");
  tabs.open("http://google.com");
}

XUL

<xul:panel>
  <xul:hbox>  
    <button onclick="doSearch ()">Button 1</button>   
  </xul:hbox>
</xul:panel>

Assuming by "redirect" you mean "navigate to"... how about this?

function doSearch (){
  var win = window.top.getBrowser();
  var tab = win.selectedBrowser;
  tab.contentWindow.location.href = "http://google.com";
}

That should be OK provided your XUL is an overlay to the ChromeWindow hosting the TabBrowser in question.

See MDN: Tabbed Browser for some other scenarios.

i would recommend this:

function doSearch (e){
var win = e.target.ownerDocument.defaultView;
  win.alert("Search!"); 
win.top.document.location.href = "http://google.com";
}

document.YOURBUTTON.addEventListener('click', doSearch, false);

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