2011-07-02 93 views
0

我需要通過單擊Firefox工具欄(作爲Firefox擴展開發)中的按鈕來打開jQuery ui對話框。我在.xul文件中添加了.js文件,但由於某種原因它不起作用。我用的是Mozilla Firefox 4,這裏是我的源代碼:在Firefox擴展中打開jQuery ui對話框

.xul文件:

.. 

<script type="application/x-javascript" src="chrome://tuttoolbar/content/tuttoolbar.js" /> 
<script type="application/x-javascript" src="chrome://tuttoolbar/content/scripts/jquery-1.4.2.min.js" /> 
<script type="application/x-javascript" src="chrome://tuttoolbar/content/scripts/jquery-ui-1.8.4.custom.min.js"/> 

... 

<toolbarbutton id="Example" tooltiptext="UI Dialog" label="Open jQ dialog" oncommand="objTutorialToolbar.sayHello1(event); event.stopPropagation();"/> 

tuttoolbar.js:

.... 

    var objTutorialToolbar = { 

    ...... 

    sayHello1 : function(aEvent) { 

    var docUrl = window.content.document.location.href; 

    var div = document.createElement("div"); 

    div.setAttribute("id", "dialog_dummy"); 

    var body = document.getElementsByTagName("body").item(0); 

    body.appendChild(div); 

    $dialog = $('#dialog_dummy').html('').dialog(
    { 
     title : 'Title', 
     modal : false, 
     autoOpen : false, 
     show : 'slide', 
     hide : 'slide', 
     url : docUrl, 
     height: 550, 
     width: 1050 
    }); 

     $dialog.dialog("open"); 
    }, 

    ... 

} 

是否有人知道哪裏是在代碼中的錯誤上面?

+0

如果你解釋了什麼不起作用,包括錯誤信息(如果有的話)也有幫助,這將有所幫助。這樣可以節省我們在代碼中查找錯誤的時間。 –

+0

有一些奇怪的例外:「TypeError:r is null」。我無法從jQuery對話框的瀏覽器中打開當前頁面... – sonjafon

回答

0

document在上面的代碼是你的代碼是在執行XUL文件。因此它不具有<body>元素,它不是一個HTML文件(document.getElementsByTagName("body")給你一個空列表)。即使它有一個,我很肯定jQuery的「對話框」是爲HTML定位系統而建立的,而不是用於XUL中的框模型。換句話說 - 它不能工作。您可能需要的是顯示<panel>元素(請參見https://developer.mozilla.org/en/XUL/panel)。或者甚至可能是一個完整的XUL對話窗口(請參閱https://developer.mozilla.org/en/XUL/dialoghttps://developer.mozilla.org/en/DOM/window.openDialog)。

+0

@ Wladimir:我刪除了以下幾行代碼:var body = document.getElementsByTagName(「body」)。item(0); body.appendChild(DIV);結果是一樣的。有個例外:「TypeError:r爲null」。我已經開發了Firefox 擴展按鈕,它使用window.open()在Javascript對話框中打開當前網頁,並且它工作正常。但是,我遇到了問題:我無法從標題欄中刪除位置。正因爲如此,我試圖創建並打開一個內聯jQuery對話框,該對話框將隱藏標題中的位置。你知道更好的方法如何做到這一點? – sonjafon

0

But, I have the problem: I can't delete location from the title bar. Because of that I've tried to create and open an inline jQuery dialog that will hide the location in title. Do you know a better way how to do that?

XUL對話框和提示可定製:

Dialogs and Prompts

Prompt Service

提示:

var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] 
.getService(Components.interfaces.nsIPromptService); 

var result = promptService.confirm(null, "Title of this Dialog", "Are you sure?"); 

// result is now true if OK was clicked, and false if cancel was clicked 

對話框:

<dialog 
    xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" 
    id="myDialogId" 
    title="My Dialog" 
    ondialogaccept="return onOK();" 
    onload="onLoad();" 
    persist="screenX screenY width height" 
    windowtype="myDialogWindowType"> 

    <script type="application/javascript" src="chrome://myext/content/mydialog.js"/> 
    <grid> 
    <columns><column/><column/></columns> 
    <rows> 
     <row align="center"><label value="Name:"/><textbox id="name"/></row> 
     <row align="center"><label value="Description:"/><textbox id="description"/></row> 
     <row align="center"><spacer/><checkbox id="enabled" label="Check to Enable"/></row> 
    </rows> 
    </grid> 
</dialog>