2

I'm working on an exam project where students will click on an option which would open two tabs with different case studies and close the previous tab but I don't have experience with Javascript and I'm unsure how to approach the problem.

It's built on Wordpress and I've tried using various plugins but all of them have failed. I have managed to get links to open in new tab (no different to but not close the previous one or open two tabs with individual webpages.

We're using anchor tags currently in HTML and putting the javascript inline but I think we might need to tackle it differently, potentially through calling a javascript file.

We want to use Javascript as it'll allow us to add code to track users when they click on the link.

The page layout is very basic with one link on it (it has a pageview conversion tracking code to track the number of students taking the exam) so there's very little chance the script will conflict with anything else, this is why the previous tab should close automatically to avoid distraction

<a href="http://www.example.com" onclick="window.open('https://www.example.com');return false">Example</a>

It opens the webpage in a new tab but I'm unsure how to make it open two new tabs (different URLs) and close the previous tab.

(I noticed there are similar questions asked but none address closing the previous tab)

2 Answers2

0

To open multiple pages at once you can use the following code.

window.open('url1.com');
window.open('url2.com');

How to make a link open multiple pages when clicked

To close the current tab/window use

window.close();

How to close current tab in a browser window?

To do all 3 actions within the inline eventhandler like your example code, you can call window.open twice with different urls, window.close(), and then return false.

JasonB
  • 6,243
  • 2
  • 17
  • 27
0

If you want to perform all 3 of those actions at the same time then you may want to create a function that does all three, and then call that function with the onclick event.

<a href="http://www.example.com" onclick="myFunction()">

Then you will define the function in a JS file and link it to your html. Something like this:

<script src="myscripts.js"></script>

The JS file (in this example) would be named myscripts.js and would look something like this:

function myFunction(){
  window.open('url1.com');
  window.open('url2.com');
  window.close()
}

Unfortunately, you are unable to close windows using Window.close() if that window was not opened by a script, so that is another issue. Without more information on how the user gets to this page in the first place, I can't really suggest a workaround.

  • Thanks! I knew the answer would be simple. As soon as I read it, I slapped my forehead haha – Andrew Hill May 27 '19 at 23:12
  • 1
    Hi Jorrin, your comment got me thinking... What if when a user opened a link, it sent them to a blank page which then opened the two windows but closed the previous one? EG: Page 1 - Link > Opens new tab (via script) > opens two tabs (via script) > Closes opened tab? This would serve the same purpose with regards to tracking etc but it would leave one tab open still. – Andrew Hill May 28 '19 at 14:51
  • Yeah that sounds like a suitable workaround. It's not the greatest in terms of UX as it gives a "90s pop-up" vibe where your browser is opening and closing windows (tabs) seemingly on its own and the user may be unsure why. This can be explained before the user clicks on the element that would trigger the reaction, so long as that doesn't further degrade the UX. – Jorrin Bruns May 28 '19 at 15:24