Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>JavaScript doesn't support clicking on an anchor (<code>&lt;a&gt;</code> tag). But, there is a workaround: with some custom JavaScript code, you can simulate the click on an anchor.</p> <p>This is a short snippet I created. It simulates the actions you'd expect, even is you use the target attribute. I built in a check to see if the method actually runs on an anchor, so you could even use this method on your buttons and expect the normal behavior. You just have to add it somewhere on your page. I tested it in Safari 5.</p> <pre><code>Element.prototype.anchorClick = function() { if (this.click) return this.click(); if (this.onclick) { var result = this.onclick(); if (!result) return result; } switch (this.target) { case '_blank': window.open(this.href); break; case '_parent': parent.location = this.href; break; case '_top': top.location = this.href; break; case '_self': case '': window.location = this.href; break; default: if (parent[this.target]) parent[this.target].location = this.href; else window.open(this.href); break; } return true; } </code></pre> <p>You use the snippet this way:</p> <pre><code>javascript:document.getElementById('anchor').anchorClick(); javascript:document.getElementsByTagName('a')[0].anchorClick(); </code></pre> <p>Here the full version with some comments inside:</p> <pre><code>Element.prototype.anchorClick = function() { // If there's a click method, the element isn't an anchor if (this.click) { // Just run the click method instead return this.click(); } // Is there an onclick method? if (this.onclick) { // Run the method and get the result var result = this.onclick(); // Isn't the result true? if (!result) { // Cancel the operation and return the result return result; } } // Check the target property switch (this.target) { // _blank means a new window case '_blank': // Open the window window.open(this.href); break; // _parent means the parent frame case '_parent': parent.location = this.href; break; // _top means the top frame case '_top': top.location = this.href; break; // _self means the current frame // When there's no value for the target property, this is the expected result case '_self': case '': window.location = this.href; break; // The last option is a user specified frame default: // Does the frame actually exist? if (parent[this.target]) { // Yep, and we'll open the page in that frame parent[this.target].location = this.href; } else { // Nope, the frame doesn't exist // The expected behaviour (in Safari) is opening a new window window.open(this.href); } break; } return true; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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