2013-12-11 31 views
0

這個網頁是爲Firefox創建的,但它必須顯示一個鏈接,它將在IE中打開。所以我試圖使用我在net上找到的代碼。但它不起作用。我錯過了什麼?無法在IE中使用nsILocalFile打開鏈接

javascript代碼:

<script type="text/javascript"> 
$(document).ready(function(){ 
    function myFunction() 
    { 
     alert("opening now...."); 
     var localFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); 
     var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess); 
     var url = content.document.location.href; 
     var args = ["-new-tab", url]; 
     localFile.initWithPath("C:\\Program Files \(x86\)\\Internet Explorer\\iexplore.exe"); 
     process.init(localFile); 
     process.run(false, args, args.length); 
    } 
}); 

HTML代碼:

<p>Click the link top open in IE</p> 
<a href="http://www.google.com/" onclick="myFunction()">open in IE</a> 

當我點擊鏈接,它會打開谷歌頁面:在同一窗口,同一borser,在同一tab.Pl建議。

謝謝。

+0

你明白,我希望,Firefox的代碼只有上述工作當作爲Firefox擴展的一部分運行,並且不會內的所有工作網頁? – EricLaw

回答

0

myFunction未在全局範圍內定義。將它添加到dcoument ready回調保證了這一點。

您可以在文檔就緒回調之外移動myFunction。但是:

在這種情況下,如下

$(function() { 
    $("a").click(function() { 
     // do stuff 
    }); 
}) 

注意此選擇的所有錨及應用相同的事件處理程序我會附上使用jquery點擊處理程序。理想情況下,你會使用選擇器。

$("#myGoogleLink").click(function() { 

}) 

的ID myGoogleLink添加到您的HTML錨

+0

是的,我現在可以執行該過程,但我得到錯誤:TypeError:Components.classes是undefined http://mywebsite/test/html_test.html 行88 – rajeev

+0

行88是:var localFile = Components.classes [「@ mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); – rajeev