目前我有兩個HTML文件。一個名爲index.html,另一個名爲editor.html在腳本標記內的不同HTML文件中運行JavaScript函數
Inside index.html我有一個iframe的editor.html上面這個iframe也是我製作的通知系統。要運行它使用我創建了一個功能的通知,可以使用像這樣:當我把它在index.html內
Notification("msg");
此功能的偉大工程,因爲函數修改的index.html的HTML代碼(通過.innerHTML)。一個問題顯示出來,當我試圖從editor.html
即使有editor.html加入到這樣的index.html在index.html的頂部叫它:
<script src="editor.html"></script>
我寫這在editor.html:
<script src="index.html"></script>
當我嘗試並運行editor.html通知功能有一個錯誤,因爲該功能是index.html的內部和修改的index.html,不編輯器html的。
請記住,在每個index.html和editor.html中,javascript都位於標記中,因爲文件中存在其他html。謝謝,如果您需要進一步確認,請提問。
一個頁面不能直接在另一個頁面上運行代碼。他們只能使用['Window.postMessage()'](https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage)進行通信和傳輸數據。從iframe內部,這將是'window.parent.postMessage()',並從iframe外的document.querySelector('iframe')。contentWindow.postMessage()'。您還需要在任何接收頁面上附加「消息」事件的事件偵聽器。 – Siguza
你可以舉個例子使用獨立的index.html和editor.html嗎?一種傳遞函數和參數的方法? –