Javascript undefine function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sebarry
    New Member
    • Jul 2007
    • 69

    Javascript undefine function

    Hi,

    I am using FCKeditor - http://www.fckeditor.n et - to allow textual input in a web page. The user can type whatever they want and then click the save button in the FCKeditor instance when they are finished. When the save button is clicked a Javascript function that I have written is executed to retrieve the user input and update the HTML form. My function is linked to the save button in the FCKeditor instance using the following line of code -

    Code:
    function FCKeditor_OnComplete( editorInstance )
    {
         editorInstance.LinkedField.form.onsubmit = doSave;
    }
    This however causes the doSave function to be called when the form is submitted.

    Is there anyway of undefining the doSave function?

    Thanks,

    Sean
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Sean.

    In fact there is!

    Since EVERYTHING in JavaScript (including functions) is an object, you can undefine or even redefine functions just like you would any other variable.

    For example, you can:
    [code=javascript]
    delete doSave;

    doSave = 5;

    doSave = function() { alert('hi!'); };
    [/code]

    and so on.

    Comment

    • Sebarry
      New Member
      • Jul 2007
      • 69

      #3
      Unfortunately no dice.

      My code is as follows:

      Code:
      function doSave()
      {
           alert( "Do Save" );
           var editor = FCKeditorAPI.GetInstance( "myFCKeditor" );
           var contentDiv = document.getElementById( contentSectionInEdit );
           contentDiv.innerHTML = editor.GetHTML();
           contentDiv.style.display = "none";
           var shortDiv = document.getElementById( shortSectionInEdit );
      		
           var idxStart = contentDiv.innerHTML.indexOf( '<H' );
           var idxEnd;
      		
           if( idxStart != -1 )
           {
              idxEnd = contentDiv.innerHTML.indexOf( '</H', idxStart );
              idxEnd = contentDiv.innerHTML.indexOf( '>', idxEnd );
              idxEnd++;
      		
              shortDiv.innerHTML = contentDiv.innerHTML.substring( idxStart, idxEnd );
           }
           else
           {
               idxStart = contentDiv.innerHTML.indexOf( '<h' );
      			
               if( idxStart != -1 )
               {
                   idxEnd = contentDiv.innerHTML.indexOf( '</h', idxStart );
                   idxEnd = contentDiv.innerHTML.indexOf( '>', idxEnd );
                   idxEnd++;
       
                   shortDiv.innerHTML = 
                      contentDiv.innerHTML.substring( idxStart, idxEnd );
                }
                else
      	shortDiv.innerHTML = contentDiv.innerHTML;
             }
      		
             shortDiv.style.display = "";
      	
             return false;
      }
      	
      function saveFormContents()
      {
              alert( "Save Form Contents" );
      		
              delete doSave;
      		
              var divToAppendTo = document.getElementById( "divToAppendToOnSubmit" );
      
              var hiddenInputId = "divContents[]";
              var existingInputs = document.getElementById( hiddenInputId );
      		
              if( existingInputs != null )
                  divToAppendTo.innerHTML = null;
      		
              var list = document.getElementById( 'sections' ); 
              var items = list.getElementsByTagName("li");
      		
              for( var i = 0, n = items.length; i < n; i++ ) 
              {
                   var hiddenField = document.createElement( "input" );
                   hiddenField.type = "hidden";
                   hiddenField.name = hiddenInputId;
                   hiddenField.id = hiddenInputId;
                   hiddenField.value = items[i].firstChild.nextSibling.innerHTML;
                   divToAppendTo.appendChild( hiddenField );
               }
       }
      	
      function FCKeditor_OnComplete( editorInstance )
      {
               editorInstance.LinkedField.form.onsubmit = doSave;
      }
      Code:
      <input type="button" value="Add New Section" onclick="javascript:addSection();" />&nbsp;&nbsp;<input type="submit" value="Save Changes" id="button_name" name="button_name" onclick="javascript:saveFormContents()" />
      When I submit the form both saveFormContent s and doSave are called. The alerts are printed, even though I have tried to delete the doSave function. I only want the doSave function to be called when the save button in FCKeditor is clicked, not when the form is submitted.

      Hope you can help.

      Sean

      Originally posted by pbmods
      Heya, Sean.

      In fact there is!

      Since EVERYTHING in JavaScript (including functions) is an object, you can undefine or even redefine functions just like you would any other variable.

      For example, you can:
      [code=javascript]
      delete doSave;

      doSave = 5;

      doSave = function() { alert('hi!'); };
      [/code]

      and so on.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Sean.

        Seems like you'll want to figure out where FCKeditor_OnCom plete() gets called. And then move or remove it.

        Comment

        • todpalin
          New Member
          • Jun 2010
          • 1

          #5
          I have found a way to basically determine if the form submit came from the FCKEditor or not.

          Code:
          function doSave(e) {
                      
              //e is null when its the FCKEditor save button
              if (e == null) {
          
          		//Do your submit on FCKEditor save button work
          
          		return false; //this disables default action (submitting the form)
          	}
          			
          //if e isn't null its a valid asp.net form button that has been pressed and we do not need to return true to allow the form to continue submitting.
          }
          
          function FCKeditor_OnComplete(editorInstance) {
          	editorInstance.LinkedField.form.onsubmit = doSave;
          }

          Comment

          Working...