Thursday, 9 February 2012

How to close child windows when closing parent window in javascript?

In many instances we want to close all child windows when main parent window is closed. This can be easily achieved using javascript.

To achieve this we need to do three thing

1) Create an array. This array is required for storing references of all child windows.


var childwindows = new Array();

2) Open child window using window.open and then store references of child window into an array

function openPopup(){
 try{
  var d = new Date();
  var win = window.open("Test.html",'test'+d.getMilliseconds(),'width=200,height=100')
  childwindows[childwindows.length] = win;
  win.focus();
 }catch(e){
  alert(e);
 }
}

3) Finally close all child windows on window.onunload event.

window.onunload = function closeChildWin(){
 for(var i=0; i<childwindows.length; i++){
  try{
     childwindows[i].close()
  }catch(e){
   alert(e);
  }
 }
}



To test this you can copy and paste following code into the file and save it as a Test.html. Let me know if it has helped you.

<html>
 <body>
        <input type="submit" onclick="openPopup()" value="Open new Window">
 </body>
</html>

<script>

var childwindows = new Array();

function openPopup(){
 try{
  var d = new Date();
  var win = window.open("Test.html",'test'+d.getMilliseconds(),'width=200,height=100')
  childwindows[childwindows.length] = win;
  win.focus();
 }catch(e){
  alert(e);
 }
}

window.onunload = function closeChildWin(){
 for(var i=0; i<childwindows.length; i++){
  try{
     childwindows[i].close()
  }catch(e){
   alert(e);
  }
 }
}

</script>

15 comments:

  1. nice article, it helped me a lot

    ReplyDelete
  2. Thanks a lot for the detailed example! Very helpful

    ReplyDelete
  3. And what happens when you refresh the parent window? Unload event will be triggered even when you refresh the parent window.

    ReplyDelete
  4. This comment has been removed by a blog administrator.

    ReplyDelete
  5. Rash from Grover's Disease can be extraordinarily irritated. Natural Treatment decrease itching ordinarily start with high-power steroid creams, for example, triamcinolone or clobetasol, beside with antihistamines. Natural Remedies for Grover's Disease provides the relief from skin disorders. It helps to reducing rashes from skin that occur from Grover’s Disease.

    ReplyDelete
  6. You should drink green tea on a regular basis because this can effective in the Natural Remedies for Granuloma Annulare natural recovery. There is another way to use green tea and that is to apply the green tea bags to your skin directly. Green Tea has anti-inflammatory and antioxidant properties which can help in the healing process.

    ReplyDelete

  7. But Dr. Itua, Traditional Herbal Practitioner in Africa, has cured HIV/Cancer which is extracted from some rare herbs. It is highly potential to cure AIDS & Cancer 100% without any residue. Dr Itua herbal medicine has already passed various blogs on how he uses his powerful herbs to heal all kinds of diseases such as. Herpes, HIV,,Cushing’s disease,Heart failure,Multiple Sclerosis,Hypertension,Colo_Rectal Cancer, Diabetes, Hepatitis,Hpv,Weak ErectionLyme Disease,Blood Cancer,Alzheimer’s disease,Bechet’s disease,Crohn’s disease,Parkinson's disease,Schizophrenia,Lung Cancer,Breast Cancer,Colo-Rectal Cancer,Blood Cancer,Prostate Cancer,siva.Fatal Familial Insomnia Factor V Leiden Mutation ,Epilepsy Dupuytren's disease,Desmoplastic small-round-cell tumor Diabetes ,Coeliac disease,Creutzfeldt–Jakob disease,Cerebral Amyloid Angiopathy, Ataxia,Arthritis,Amyotrophic Lateral Scoliosis,Fibromyalgia,Fluoroqquinolone Toxicity,Brain Cancer,Breast Cancer,Lung Cancer,Kidney Cancer,Syndrome Fibrodysplasia Ossificans ProgresS sclerosis,Seizures,Alzheimer's disease,Adrenocortical carcinoma.Asthma,Allergic diseases.Hiv_ Aids,Herpe ,Copd,Glaucoma., Cataracts,Macular degeneration,Cardiovascular disease,Lung disease.Enlarged prostate,Osteoporosis.Alzheimer's disease,

    Dementia.,Wart Remover,Cold Sore, Epilepsy, also his herbal boost immune system as well. I'm telling this because he uses his herbal medicine to cure me from hepatitis B and HIV, which I have been living on for 9 months now with no side effects. Herbal Medicine is just as good when drinking it although i have to use the restroom after drinking it which I don't really care about because i just want to get the virus out of my body, I will recommend Dr Itua to anyone sick out here to contact Dr Itua with this following information.

    Email...drituaherbalcenter@gmail.com /
    Web: www.drituaherbalcenter.com
    He might be late to respond because he is always busy with patents, but he will surely get back to you with a positive response.

    ReplyDelete
  8. https://thisiswhatiknowabout.blogspot.com/2012/02/how-to-close-child-windows-when-closing.html?showComment=1648538494820#c5885426627865007798

    ReplyDelete
  9. Herbal Supplement for Actinic Keratosis is highly recommended to all people suffering from these diseases as they offer positive results at an affordable price. Herbal Remedies for Actinic Keratosis, It immediately reduced redness.The extract of olive leaf has been used for thousands of years to treat various skin conditions. Herbal Remedies for Actinic Keratosis, It immediately reduced redness.

    ReplyDelete
  10. One of several ways you can treat granuloma annulare is using a potent and effective Herbal Remedies for Granuloma Annulare. Herbal supplements for granuloma annulare include milk thistle, borage oil, dandelion root, red clover extract, burdock root, white willow bark, and St. John’s wort, among others. It would help if you chose a 100% natural herbal supplement that contains only pure herbs without fillers or additives.

    ReplyDelete
  11. The typical symptom of Hydrocele is enlargement or swelling around a testicle.The Natural Remedies for Hydrocele involves surgery to correct any anatomical defect in your child’s scrotum. These additional Herbal Remedies for Hydrocele include herbal medicine like Celdeton Hydrocele.Natural Cure Zone strongly recommends that you consult with your primary healthcare provider before starting any Herbal Supplements.Herbal Supplement for Hydrocele can be treated naturally with herbal supplements.

    ReplyDelete