0

I was looking for a solution to my typical problem and finally I found one but I need to make it more appropriate. My problem is I want to open a link in new tab if it is not already open, the example I found from Mozilla doc is doing exactly what I need but it is opening new link in a window not in tab Can someone here help me how can I change this code to open link in new tab instead of window

My Code :

<script type="text/javascript">
var windowObjectReference = null; // global variable

function openFFPromotionPopup() {
  if(windowObjectReference == null || windowObjectReference.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  {
    windowObjectReference = window.open("https://jobscane.com/",
   "PromoteFirefoxWindowName", "resizable,scrollbars,status");
    /* then create it. The new window will be created and
       will be brought on top of any other window. */
  }
  else
  {
    windowObjectReference.focus();
    /* else the window reference must exist and the window
       is not closed; therefore, we can bring it back on top of any other
       window with the focus() method. There would be no need to re-create
       the window or to reload the referenced resource. */
  };
}
</script>

(...)

<p><a
 href="https://jobscane.com/"
 target="_blank"
 onclick="openFFPromotionPopup(); return false;" 
 title="This link will create a new window or will re-use an already opened one"
>Promote Firefox adoption</a></p>
Syed Saqlain
  • 544
  • 4
  • 21
  • There is at least one good reason why this functionality doesnt work -- such code can be used to force tabs to keep opening whenever a user tries to close them.. Why cant you just load the promotional in an IFRAME like everyone else? You can even use a 'lightbox' to display the IFRAME and make it appear to pop up right in front of the screen... HTML5 has many newer features that dont annoy users to their wits end. – mike510a Sep 22 '18 at 05:07

2 Answers2

1

To open in a new tab, you have to use an old workaround.

See this post:

Open a URL in a new tab (and not a new window) using JavaScript

mike510a
  • 2,102
  • 1
  • 11
  • 27
  • If you're just going to link to another SO question, you should just vote to close as a duplicate. – Barmar Sep 22 '18 at 06:43
0

You can use Anchor tag instead of window.open()

Try something like following and see if it works. Also, the problem is behavior might vary from browser to browser and on user settings.

<a href={link} target="_blank">Open in tab</a>
Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19