3

Opening a JavaScript-based link in a new window/tab.

I have a link

<a href='/page' onclick='trackClick()'>Page</a>

What I believe is happening here is that the trackClick() function may not have enough time to execute before browser redirects to a new page. (BTW: Am I wrong here?)

To workaround that, I have changed the html to

<a href='#' onclick='trackClick('/page')'>Page</a>

where trackClick function would redirect to a next page after it has sent a signal to analytics endpoint to avoid racing condition.

As a result, "shift+click" or "right+click open in a new window" now will not open the the /page in a new window/tab.

What's the best way to overcome this? How likely for the browser to not wait for the onclick event to finish before redirecting to a next page?

thank you

0

1 Answer 1

3

The short answer is, onclick != right click.

For a quick and dirty demo

<script>
  function leftClick() {
    alert("click");
  }

  function rightClick() {
    alert("right click");
  }

  function mouseDown() {
    alert("Mouse down");
  }

  function mouseUp() {
    alert("Mouse up");
  }
</script>
<a href="http://www.google.com" onclick="leftClick()">CLick Me</a><br>
<a href="http://www.google.com" oncontextmenu="rightClick()">Right CLick Me</a><br>
<a href="http://www.google.com" onmousedown="mouseDown()">Any Click Me</a><br>
<a href="http://www.google.com" onmouseup="mouseUp()">Any Click Me</a><br>

So what you actually want is:

<a href='/page' onclick='trackClick()' oncontextmenu="trackClick()">Page</a>

Or maybe

<a href='/page' onmouseup='trackClick()' >Page</a> 

Better sill look at using unobtrusive handlers and the mousedown and mouseup events

How likely for the browser to not wait for the onclick event to finish before redirecting to a next page?

The onclick will complete. If you were to return false from a click handler the navigation will not occur at all. Demo

Sign up to request clarification or add additional context in comments.

7 Comments

arguably, contextmenu is not right click either... You can very well trigger a contextmenu from an other input (some keyboards have a contextmenu key). Right click would be mousedown=>e.buttons===2 + mouseup=>e.buttons===2
@Kaiido, you are correct, I was trying to keep it simple, perhaps I've over simplified it.
Well the real problem is that your code won't track page opening per se, and if user opens the contextmenu multiple times, without ever clicking "open in new tab/window", you'll have tracked false positives. (same for mouseup, + if you don't check that mousedown was also made on the same element, you'll also track false clicks)
Yeah, we know nothing about trackClick() it could be attempting to trace the element clicked on a page to cause a navigation event, not the target page load.
In that case setting the href directly to the correct value would be the correct solution.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.