Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript get href when link clicked
    primarykey
    data
    text
    <p>Here is my situation. I have some links throughout a website. Some of them look like this:</p> <p><code>&lt;a target="_blank" onclick="someFunction()" href="/somFile.pdf"&gt;some file&lt;/a&gt;</code></p> <p>and some look like this:</p> <p><code>&lt;a target="_blank" href="/somFile.pdf"&gt;some file&lt;/a&gt;</code></p> <p>All of the links should be calling someFunction() when clicked. The ones that have the call in the onclick attribute are legacy content pages. Newer pages have a jQuery click event attached to them like so:</p> <pre><code>$(document).ready(function(){ $('a[href$=".pdf"]').click(function() { someFunction(); }); }); </code></pre> <p>So here's the thing. I can update someFunction(), but I cannot touch the actual links or that jQuery. What I need is to know the href value of the link clicked. I have tried using the following within someFunction() :</p> <pre><code>var ev = window.event; origEl = ev.target || ev.srcElement; console.log(origEl.href); </code></pre> <p>but this does not work. I also tried <code>console.log(window.event)</code> and get nothing, says it is undefined. Any idea what I'm missing here, or is it basically impossible without passing a reference to it when the function is called?</p> <p>edit: to be clear, I cannot as a short or even medium term solution edit the call to <code>someFunction()</code> in the onclick or in the jQuery code black, so I cannot for instance change them to <code>someFunction(this)</code> or similar. I'm not sure it is possible to get the href from within someFunction() unless I do that though :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload