最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "InstallTrigger" is not defined - Stack Overflow

programmeradmin2浏览0评论

In my html page i have code something like this, where i have installed an extension only if the browser is Firefox:

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
{
  //relevant code
  InstallTrigger.install(InstallXPI);
}

It works fine in every browser. But when the same page is used through htmlunit framework and using browserversion.FIREFOX_3_6 argument in webclient. It shows an error there:

.gargoylesoftware.htmlunit.ScriptException: Wrapped 
.gargoylesoftware.htmlunit.ScriptException: Wrapped 
.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "InstallTrigger" is not defined.

Any idea about this?

In my html page i have code something like this, where i have installed an extension only if the browser is Firefox:

if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
{
  //relevant code
  InstallTrigger.install(InstallXPI);
}

It works fine in every browser. But when the same page is used through htmlunit framework and using browserversion.FIREFOX_3_6 argument in webclient. It shows an error there:

.gargoylesoftware.htmlunit.ScriptException: Wrapped 
.gargoylesoftware.htmlunit.ScriptException: Wrapped 
.gargoylesoftware.htmlunit.ScriptException: ReferenceError: "InstallTrigger" is not defined.

Any idea about this?

Share Improve this question edited Feb 1, 2012 at 7:14 Wladimir Palant 57.7k12 gold badges99 silver badges127 bronze badges asked Feb 1, 2012 at 7:08 Prateek SharmaPrateek Sharma 3463 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

This is a reminder for you: don't use browser detection, use feature detection. The issues with your code:

  • InstallTrigger is a feature of the Gecko engine, not Firefox. You are explicitly looking for "Firefox" in the user agent string however and might exclude other browsers based on the Gecko engine (there are e.g. SeaMonkey, K-Meleon, Camino).
  • User agent strings can be spoofed which is apparently what htmlunit is doing - it claims to be Firefox despite using a different browser engine. Your code will run into trouble then.

Here is how you would do it properly:

if ("InstallTrigger" in window)
{
  // Gecko platform, InstallTrigger available
  InstallTrigger.install(InstallXPI);
}
发布评论

评论列表(0)

  1. 暂无评论