在網路開發中,常用到的一個場景是父頁面開啟一個子頁面,當子頁面被關閉時需要做一些操作,例如刷新父頁面等。而在使用JavaScript編寫Web應用程式時,我們需要知道如何在父頁面中取得子頁面的狀態,以便於做出對應的操作。
在JavaScript中,可以使用window物件來開啟和關閉頁面。當我們開啟一個新頁面時,可以使用window.open()方法。此方法接受三個參數,第一個參數是要開啟的頁面位址,第二個參數是視窗的名稱,第三個參數是視窗的特性。
例如,下面的程式碼開啟了一個新窗口,並將視窗名稱設定為「myWindow」:
var myWindow = window.open("http://www.example.com", "myWindow", "width=500,height=500");
當我們想要取得子頁面是否關閉時,可以使用子頁面的window對象。在子頁面中,可以使用window.onunload事件來偵測頁面是否已關閉。當頁面被關閉時,可以向父頁面發送一個訊息。
下面是一個子頁面的JavaScript程式碼範例:
window.onunload = function() { window.opener.postMessage("childClosed", "*"); }
在這個範例中,當子頁面被關閉時,它發送了一個名為「childClosed」的訊息給父頁面。訊息的參數是一個字串,可以是任何內容。第二個參數是一個來源參數,用於指定訊息的發送方。
在父頁面中,可以新增一個事件監聽器來接受來自子頁面的訊息。當訊息接收到時,可以執行對應的操作。下面是一個範例程式碼:
window.addEventListener("message", receiveMessage, false); function receiveMessage(event) { if (event.data === "childClosed") { // 子页面已关闭,执行相应操作 } }
在這個程式碼中,當訊息接收到時,會呼叫一個名為receiveMessage的函數。在該函數中,檢查訊息是否為「childClosed」字串,如果是,請執行對應的操作。
總的來說,使用JavaScript在父頁面中取得子頁面是否關閉,我們需要在子頁面中新增一個事件監聽器來傳送訊息,然後在父頁面中新增一個事件監聽器來接受訊息並執行對應操作。
以上是javascript如何在父頁中取得子頁面的狀態的詳細內容。更多資訊請關注PHP中文網其他相關文章!

No,youshouldn'tusemultipleIDsinthesameDOM.1)IDsmustbeuniqueperHTMLspecification,andusingduplicatescancauseinconsistentbrowserbehavior.2)Useclassesforstylingmultipleelements,attributeselectorsfortargetingbyattributes,anddescendantselectorsforstructure

html5aimstoenhancewebcapabilities,Makeitmoredynamic,互動,可及可訪問。 1)ITSupportsMultimediaElementsLikeAnd,消除innewingtheneedtheneedtheneedforplugins.2)SemanticeLelelemeneLementelementsimproveaCceccessibility inmproveAccessibility andcoderabilitile andcoderability.3)emply.3)lighteppoperable popperappoperable -poseive weepivewebappll

html5aimstoenhancewebdevelopmentanduserexperiencethroughsemantstructure,多媒體綜合和performanceimprovements.1)SemanticeLementLike like,和ImproVereAdiability and ImproVereAdabilityActibility.2)and tagsallowsemlessallowseamelesseamlessallowseamelesseamlesseamelesseamemelessmultimedimeDiaiaembediiaembedplugins.3)。 3)3)

html5isnotinerysecure,butitsfeaturescanleadtosecurityrisksifmissusedorimproperlyimplempled.1)usethesand andboxattributeIniframestoconoconoconoContoContoContoContoContoconToconToconToconToconToconTedContDedContentContentPrenerabilnerabilityLikeClickLickLickLickjAckJackJacking.2)

HTML5aimedtoenhancewebdevelopmentbyintroducingsemanticelements,nativemultimediasupport,improvedformelements,andofflinecapabilities,contrastingwiththelimitationsofHTML4andXHTML.1)Itintroducedsemantictagslike,,,improvingstructureandSEO.2)Nativeaudioand

使用ID選擇器在CSS中並非固有地不好,但應謹慎使用。 1)ID選擇器適用於唯一元素或JavaScript鉤子。 2)對於一般樣式,應使用類選擇器,因為它們更靈活和可維護。通過平衡ID和類的使用,可以實現更robust和efficient的CSS架構。

html5'sgoalsin2024focusonrefinement和optimization,notNewFeatures.1)增強performanceandeffipedroptimizedRendering.2)inviveAccessibilitywithRefinedwithRefinedTributesAndEllements.3)explityconcerns,尤其是withercercern.4.4)

html5aimedtotoimprovewebdevelopmentInfourKeyAreas:1)多中心供應,2)語義結構,3)formcapabilities.1)offlineandstorageoptions.1)html5intoryements html5introctosements introdements and toctosements and toctosements,簡化了inifyingmediaembedingmediabbeddingingandenhangingusexperience.2)newsements.2)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1
強大的PHP整合開發環境

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

Dreamweaver Mac版
視覺化網頁開發工具