Closing current window + infinite tab loop help

So i’m making a infinite tab loop. Currently the codepen doesn’t have jQurey added so your safe (it uses document ready function) --> https://codepen.io/Mike-was-here123/pen/MOeexP

My only problem is that i cant close the current tab. All i want it to do is open a new tab, and then close the current tab. So far a new tab opens (of itself), which then later opens a new tab of itself. The only problem is, well, i cant get the current tab to close. Then i’m stuck with infinite opening tabs. The hard part is, you can only try one run of the edited code before you need to completely restart your “internet explorer” (task manager). Is their any edit to my current program that i know will make it so the current tabs closes after a new one has been opened? All i need help with is closing the current tab.

If you go and decide to change the code you will:

  1. Make sure you don’t have any important tabs opened (besides this one)
  2. Need to allow :cookie:'s
  3. Need to add jQurey
  4. Hope it works

This is the code in advance

 $(document).ready(function() {
  if (document.URL !== "https://codepen.io/Mike-was-here123/pen/MOeexP") {
    window.open("https://codepen.io/Mike-was-here123/pen/MOeexP");
  }
  var one = 1;
  if (1 === 1) {
    window.open("https://codepen.io/Mike-was-here123/pen/MOeexP");
    one += 1;
    window.close();
  }
// my backup closing which still doesn't close.
  if (one !== 1) {
    self.close();
  }
}); // https://codepen.io/Mike-was-here123/full/dZXXWx/ });

Help from any brave coders who want to risk it?

Hi, you can close a browser window you opened yourself via the window.open command. But you must save a reference to the window you opened.

See example below:

<body>
  <a href="#" onClick="openWindow()">Open Window</a>
  <br>
  <a href="#" onClick="closeWindow()">Close Window</a>
</body>
  <script>
  var myWindow;
    
    function openWindow(){
      myWindow = window.open('http://google.com');
    }
    
    function closeWindow(){
      myWindow.close();
    }
  </script>
1 Like

Have you tried debugging? Place breakpoint on self.close() and see if it’s being hit.

1 Like

Well you cant debug if their opening a couple per second.

Its should by logic close.

Well its going to do it automatically when you open the window, is their any way i can use that as a reference? I already tried a check variable. Why cant it close without a reference if so?

I even tried adding window.close() to the opening tab if statement, and i had to delete the pen because it didn’t work. New pen posted.

Console says

 ReferenceError: $ is not defined

You don’t have jquery loaded.

IIRC you can’t do anything that affects the current tab such as killing or reloading using a pen since they’re shown via iframe. Debug view removes this limitation.

what exactly are you trying to accomplish with your code?

I don’t have jquery loaded because it would cause tabs to open uncontrollably every time you open the pen (if cookies enabled). I disabled jQurey until i can find a way to close the current tab ( i know how to infinite open one already). Basically i’m trying to open a new tab, and close the current. My code itself opens a new tab of itself, thus, opens a new tab of itself, etc. Now i want to open a new tab AND close the current so you don’t have 419 tabs at once. How can i accomplish this?

I was about to say the same thing.

That’s not really helpful. Post it in a code pen if you’ll be posting that much code. You don’t even have an explanation for it.

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

Try this:

 $(document).ready(function() {
  if (document.URL !== "https://codepen.io/Mike-was-here123/debug/MOeexP") {
    window.open("https://codepen.io/Mike-was-here123/debug/MOeexP"); ////Open in debug mode
  }
  var one = 1;
  if (1 === 1) {
    window.open("https://codepen.io/Mike-was-here123/debug/MOeexP");
    one += 1;
    window.close();
  }
// my backup closing which still doesn't close.
  if (one !== 1) {
    self.close();
  }
}); // https://codepen.io/Mike-was-here123/full/dZXXWx/ });

Please start your own post

Wouldn’t that just open a bunch of debug tabs?

Well, you can’t close a tab in Codepen unless you’re in debug mode. window.close does nothing outside of debug mode. This will open a bunch of debug tags but they’ll also close themselves. It’s still going to open faster than it closes but at least it closes.

Oh okay ill try this.

It closes too fast, works though

Anyway i can open as fast as closing, maybe with a slight delay before closing?

what if i just added a loop that closes window at even numbers, and opens at off?

Update: I made it so it opens 50 tabs then closes 50. You can edit the amount to.

I might add it so you can select how many tabs you want to do.

Here was the code:

$(document).ready(function() {
  if (document.URL !== "https://codepen.io/Mike-was-here123/debug/MOeexP") {
     window.open("https://codepen.io/Mike-was-here123/debug/MOeexP"); ////Open in debug mode
  }
 
  for (var i = 0; i < 50; i++) {
    if (i % 2 === 0) {
       window.open("https://codepen.io/Mike-was-here123/debug/MOeexP");
    } 
     if (i % 2 === 1) {
      window.close();
    }
  }

});