Search code examples
javascriptjakarta-eedom-eventshref

Simulate the href click in Javascript function?


I have below code in my legacy project in jsp:

<tr>
    <th colspan="2">Customer</th>
    <td colspan="2">
      <a href="myAction.do" target="view">CustomerLink</a></td>
</tr>

I want little modification in behaviour when we click CustomerLink. I just want to do some processing in Javascript function before hitting the action. I am not getting how to simulate the href link behaviour in Javascript function?

What I tried:-

<tr>
    <th colspan="2">Customer</th>
    <td colspan="2">
      <a href="javascript:customerLinkClicked('myAction.do')" target="view">CustomerLink</a></td>
</tr>

// Javascript function

function customerLinkClicked(path)
{

    // simulate the href link behaviour, not sure how to do this


}

Solution

  • <a href="javascript:customerLinkClicked('myAction.do')" target="view">CustomerLink</a>
    <script>
        function customerLinkClicked(path) {
            if (confirm('message'))
                document.location.href = path;
        }
    </script>