javascripthtmlanchorurl-scheme

Why does my “javascript:” link navigate away from the page when clicked?


<a href="javascript:document.getElementById('create_table').style.display='block'">Insert Table</a>

The code works perfectly fine in Google Chrome but in Internet Explorer and Firefox it just redirects to a page with the text "block"


Solution

  • You should never use the javascript: pseudoprotocol. Use the click event for this. Besides, also watch the quotes.

    Here's the correct approach:

    <a href="#" onclick="document.getElementById('create_table').style.display='block'; return false;">Insert Table</a>
    

    Note that I (optionally) returned false here to block the default action.