473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Javascript undefine function

69
Hi,

I am using FCKeditor - http://www.fckeditor.net - 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 -

Expand|Select|Wrap|Line Numbers
  1. function FCKeditor_OnComplete( editorInstance )
  2. {
  3.      editorInstance.LinkedField.form.onsubmit = doSave;
  4. }
  5.  
This however causes the doSave function to be called when the form is submitted.

Is there anyway of undefining the doSave function?

Thanks,

Sean
Oct 13 '07 #1
4 6639
pbmods
5,821 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. delete doSave;
  2.  
  3. doSave = 5;
  4.  
  5. doSave = function() { alert('hi!'); };
  6.  
and so on.
Oct 13 '07 #2
Sebarry
69
Unfortunately no dice.

My code is as follows:

Expand|Select|Wrap|Line Numbers
  1. function doSave()
  2. {
  3.      alert( "Do Save" );
  4.      var editor = FCKeditorAPI.GetInstance( "myFCKeditor" );
  5.      var contentDiv = document.getElementById( contentSectionInEdit );
  6.      contentDiv.innerHTML = editor.GetHTML();
  7.      contentDiv.style.display = "none";
  8.      var shortDiv = document.getElementById( shortSectionInEdit );
  9.  
  10.      var idxStart = contentDiv.innerHTML.indexOf( '<H' );
  11.      var idxEnd;
  12.  
  13.      if( idxStart != -1 )
  14.      {
  15.         idxEnd = contentDiv.innerHTML.indexOf( '</H', idxStart );
  16.         idxEnd = contentDiv.innerHTML.indexOf( '>', idxEnd );
  17.         idxEnd++;
  18.  
  19.         shortDiv.innerHTML = contentDiv.innerHTML.substring( idxStart, idxEnd );
  20.      }
  21.      else
  22.      {
  23.          idxStart = contentDiv.innerHTML.indexOf( '<h' );
  24.  
  25.          if( idxStart != -1 )
  26.          {
  27.              idxEnd = contentDiv.innerHTML.indexOf( '</h', idxStart );
  28.              idxEnd = contentDiv.innerHTML.indexOf( '>', idxEnd );
  29.              idxEnd++;
  30.  
  31.              shortDiv.innerHTML = 
  32.                 contentDiv.innerHTML.substring( idxStart, idxEnd );
  33.           }
  34.           else
  35.     shortDiv.innerHTML = contentDiv.innerHTML;
  36.        }
  37.  
  38.        shortDiv.style.display = "";
  39.  
  40.        return false;
  41. }
  42.  
  43. function saveFormContents()
  44. {
  45.         alert( "Save Form Contents" );
  46.  
  47.         delete doSave;
  48.  
  49.         var divToAppendTo = document.getElementById( "divToAppendToOnSubmit" );
  50.  
  51.         var hiddenInputId = "divContents[]";
  52.         var existingInputs = document.getElementById( hiddenInputId );
  53.  
  54.         if( existingInputs != null )
  55.             divToAppendTo.innerHTML = null;
  56.  
  57.         var list = document.getElementById( 'sections' ); 
  58.         var items = list.getElementsByTagName("li");
  59.  
  60.         for( var i = 0, n = items.length; i < n; i++ ) 
  61.         {
  62.              var hiddenField = document.createElement( "input" );
  63.              hiddenField.type = "hidden";
  64.              hiddenField.name = hiddenInputId;
  65.              hiddenField.id = hiddenInputId;
  66.              hiddenField.value = items[i].firstChild.nextSibling.innerHTML;
  67.              divToAppendTo.appendChild( hiddenField );
  68.          }
  69.  }
  70.  
  71. function FCKeditor_OnComplete( editorInstance )
  72. {
  73.          editorInstance.LinkedField.form.onsubmit = doSave;
  74. }
  75.  
Expand|Select|Wrap|Line Numbers
  1. <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()" />
  2.  
When I submit the form both saveFormContents 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

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:
Expand|Select|Wrap|Line Numbers
  1. delete doSave;
  2.  
  3. doSave = 5;
  4.  
  5. doSave = function() { alert('hi!'); };
  6.  
and so on.
Oct 13 '07 #3
pbmods
5,821 Expert 4TB
Heya, Sean.

Seems like you'll want to figure out where FCKeditor_OnComplete() gets called. And then move or remove it.
Oct 13 '07 #4
I have found a way to basically determine if the form submit came from the FCKEditor or not.

Expand|Select|Wrap|Line Numbers
  1. function doSave(e) {
  2.  
  3.     //e is null when its the FCKEditor save button
  4.     if (e == null) {
  5.  
  6.         //Do your submit on FCKEditor save button work
  7.  
  8.         return false; //this disables default action (submitting the form)
  9.     }
  10.  
  11. //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.
  12. }
  13.  
  14. function FCKeditor_OnComplete(editorInstance) {
  15.     editorInstance.LinkedField.form.onsubmit = doSave;
  16. }   
Jun 18 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: sam | last post by:
HI, I installed php4 for apached and restart apache afterward. but my little php script generated error followint error: PHP Notice: Undefined index: myname in...
1
by: Rafal 'Raf256' Maj | last post by:
Hi, is there a commend to undefine all macros #define inside a file? in example: #define FOR(x) for (..............) #define MyMacro1(x,y) ........... // ... code ...
14
by: Rich | last post by:
I am converting my enterprise solution from VS 2003 (.NET v1.1.4322) to VS 2005 (.NET v2.0.50727). The entire solution uses serveral technologies - Windows Server 2003 (AD, SQL Server 2000, IIS,...
7
by: Alden Pierre | last post by:
Hello, I'm trying to create my own user define container, but I'm having a little hard time figuring out why is my class considered undefined by my compiler. Here is the following code. //...
5
by: Rocky86 | last post by:
hi people basically I am having a problem with the followinng code: $names=sizeof($temp); $report="total=$names&"; foreach($temp as $list) $report.="name".$names--."=".$list."&"; echo...
6
by: Pietro Cerutti | last post by:
Hi Group, is there a mean to undefine a function, in a similar way as you can undefine macros? For example, let's say that I need a few declarations from stdio.h but want to define my own...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.