little Javascript help

Discussion in 'Web Design and Development' started by Cabbit, Mar 2, 2008.

  1. Cabbit macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #1
    how do i get it so that my script wont error out when the element editor isnt present?

    Code:
    <script type="text/javascript">
    			window.onload = function()
    				{
    				var oFCKeditor = new FCKeditor( 'editor' ) ;
    				oFCKeditor.BasePath = "../editor/" ;
    				oFCKeditor.ReplaceTextarea() ;
    				}
    		</script>
     
  2. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland
    #3
    this is what i tryed

    Code:
    try
      {
    window.onload = function()
    				{
    				var oFCKeditor = new FCKeditor( 'editor' ) ;
    				oFCKeditor.BasePath = "../editor/" ;
    				oFCKeditor.ReplaceTextarea() ;
    				}
    }
    catch(err)
      {
    }
    
    witch is the best i could work out how to stop the error, but my javascript skills is really limited.
     
  3. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #4
    This code will assume 'editor' is an id attribute for a tag in the document. You can add an else statement if you have something else to do if it's not present.
    Code:
    window.onload = function()
    {
      if (document.getElementById('editor')) {
        var oFCKeditor = new FCKeditor( 'editor' ) ;
        oFCKeditor.BasePath = "../editor/" ;
        oFCKeditor.ReplaceTextarea() ;
      }
    }
     
  4. Cabbit thread starter macrumors 68020

    Cabbit

    Joined:
    Jan 30, 2006
    Location:
    Scotland

Share This Page