2014-04-03 20 views
0

我在讀取和寫入mozilla xul文件時遇到了問題。 起初,我想簡單地閱讀文件路徑(檢查是否I/O工作) 所以我寫了這個代碼使用mozilla xul讀取和寫入文件

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="window" title="title"> 
<script> 
Components.utils.import("resource://gre/modules/FileUtils.jsm"); 

var file = FileUtils.getFile("Desk", ["temp.xml"]); 
alert(file.path); 
</script> 
</window> 

它應該顯示警報窗口與路徑temp.xml(該文件存在於桌面)。但它在Mozilla Firefox中沒有顯示任何內容。 有什麼問題?

回答

1

我的Firefox說明兩個問題:

  1. 它扼流圈警報()錯誤:

    Error: Cannot call openModalWindow on a hidden window = NS_ERROR_NOT_AVAILABLE Source file: resource://gre/components/nsPrompter.js Line: 382

  2. 你的窗口沒有風格,所以它的 「隱形」

這是一個解決方法,在瀏覽器控制檯中顯示路徑而不是文本框中的路徑

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<?xml-stylesheet href="chrome://global/skin/global.css" type="text/css"?> 
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" id="window" title="title"> 
    <textbox id="text" value="N/A"/> 
<script type="application/javascript"> 
Components.utils.import("resource://gre/modules/FileUtils.jsm"); 

var file = FileUtils.getFile("Desk", ["temp.xml"]); 
document.getElementById("text").value = file.path; 
console.log(file.path); 
//alert(file.path); 
</script> 
</window>