'JavaScript onclick requiring two clicks to open new tab

I have a piece of JavaScript that is suppose to open a new tab or show a popup overlay on the current page depending on certain conditions.

In order to determine which action to do, I need to append two scripts to the DOM during the onclick event.

The reason the scripts are appended during the onclick is due to the low click rate on the element.

Right now, the overlay behavior works as expected but when a new tab needs to be opened, it requires two clicks instead of one.

Below is the snippet of code for the onclick event:

button_link.onclick = function () {
  var _this = this;
  appendScript('http://example.com/script1.js', function () {
    appendScript('http://example.com/script2.js', function () {

      if (isNewWindow() || fitsDimensions(button_link.offsetHeight) || (script1Val && !script2Val)) {
        // Open a new tab
        button_link.href = button_link.href.replace('false', 'true'); // make sure param is true
        _this.onclick = 'return '+true;
        return true;
      }

      _this.onclick = 'return '+false;

      appendScript(URL_SCHEME+'ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js', function () {
        appendScript('http://example.com/script3.js', function () {
          // Call function from script3.js to create popup overlay
        });
      });

      return false;

    });
  });

  return false;
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source