mattroberts33
mattroberts33

Reputation: 280

How to use jQuery to load pages in new window in order then close them?

I have a CMS and in order for the tags that this CMS uses to work I need to load all of the pages, including some that aren't found directly on the site. This is a one time thing so it doesn't really matter, but I thought it would be fun to make a function for this.

I want to create a link that when clicked will open and load a series of pages in order and then close them.

<a href="#" id="#load">Click here to load the pages!</a>

<script>
    $('#load').click(function() {
        window.open('www.google.ca');
    });
</script>

The above code should open one page, how would I add more pages and can I make the script close the pages?

Upvotes: 0

Views: 47

Answers (2)

devqon
devqon

Reputation: 13997

var sites = ["www.google.ca", "www.google.com"];

$("#load").click(function(){
    var windows = [];
    for(var i = 0; i < sites.length; i++){
        var wd = window.open("http://" + sites[i]);
        windows.push(wd);
    }
    setTimeout(function(){
        for(var i = 0; i < windows.length; i++){
            windows[i].close();
        }
    }, 1000);
});

JSFIDDLE

BUT, as A. Wolff said, the browsers block windows if you open more then 1 and only works if user enables the popups for the site.

Upvotes: 1

Pramod S. Nikam
Pramod S. Nikam

Reputation: 4549

You can declare array to hold all windows. And iterate it to close like below:

var window_arr = new Array();

<script>

 // Code to open all windows    

  $('#load').click(function() {
    window_arr.put(window.open('www.google.ca'));
  });

// code to close all windows

 for (var window in  window_arr ) {
  window_arr[window].close();
  }

Upvotes: 1

Related Questions