Javascript works in Chrome but not IE or Firefox

firefoxgoogle-chromeinternet explorerjavascriptscripting

I am using Javascript within an HTML file to expand and collapse elements of the file.

This is the script:

function toggleBlock(pstrID){
  var myDiv = document.getElementById('d' + pstrID);
  if (myDiv){
    if (myDiv.style.display == 'none'){
      showBlock(pstrID);
    } else{
      hideBlock(pstrID);
    }
  }
}
function showBlock(pstrID){
  var myDiv = document.getElementById('d' + pstrID);
  if (myDiv){
    myDiv.style.display = 'block';
    var myImage = document.getElementById('i' + pstrID);
    if (myImage){
      myImage.src = 'arrowdown.gif';
      myImage.alt = 'Hide';
    }
    if (document.location.href.indexOf('mk:@') == 0)
      myDiv.innerHTML = myDiv.innerHTML;
  }
}
function hideBlock(pstrID){
  var myDiv = document.getElementById('d' + pstrID);
  if (myDiv){
    myDiv.style.display = 'none';
    var myImage = document.getElementById('i' + pstrID);
    if (myImage){
      myImage.src = 'arrowright.gif';
      myImage.alt = 'Show';
    }
    if (document.location.href.indexOf('mk:@') == 0)
      myDiv.innerHTML = myDiv.innerHTML;
  }
}

When I call the script, I use the following:

<a id="h7217" class="expandingblocktemplate" title="" href="javascript:toggleBlock('7217')">
  • In Chrome everything works fine.

  • In IE, clicking the link leads to a different window (address shown is javascript:toggleBlock('7217') obviously, the number depends on the link that's clicked) and the error "Internet Explorer cannot display the webpage".

  • In Firefox, a new tab appears and the Error Console says:
    Error: toggleBlock is not defined
    Source File: javascript:toggleBlock('7217')
    Line: 1

Best Answer

just add

      return false ; 

after the toggleBlock call.