0

I'm trying to call a JavaScript function from an ASP.NET MVC view (.html) using Action Link/Hyperlink()/Link Buttons. Left click works good, but right clicking the link and choosing 'Open a New Tab or Page' is not working.

My attempt so far has been:

<a href="#" onclick=aFunction()>link</a>

    aFunction(object)
    {
       alert('Inside Javascript Function')
    }
alex
  • 6,818
  • 9
  • 52
  • 103

2 Answers2

0

This answer might help.

Try giving your hyperlink an id attribute (say, id="foo"), and create a click handler that performs a specified action whenever a#foo is right-clicked:

document.getElementById("foo").onmousedown = function(event) {
    if (event.which == 3) {
        alert('Inside Javascript Function');
    }
}

EDIT:

Per your comment:

I don't want to execute the function as soon as the user right click. I want execute the function after the user right click the link and click open new tab option.

I don't believe this is possible (unless there is some arcane Chrome JavaScript API that detects such an event). It wouldn't make sense for JavaScript to know how Chrome works. Further, JavaScript is sandboxed in such a way that it doesn't have access to the user's local file system - by extension, it should not know what the Google Chrome process itself is doing.

alex
  • 6,818
  • 9
  • 52
  • 103
  • That is look close to my goal but I dont want to execute the function as soon as the user right click. I want execute the function after the user right click the link and click open new tab option. Thanks. – G. Ashu Oct 12 '17 at 15:07
  • @G.Ashu I think this is impossible. Please see my updated answer. – alex Oct 12 '17 at 15:27
0

Use the approach mentioned in this answer: How to run function on new tab from main tab? (Google Chrome)

Basically you would store the function in local storage and on page load event of the page this links open, you would check if there is a function stored in the local storage, and if so, then execute it.

Gurvinder Singh
  • 91
  • 2
  • 10