A simple UBB editor has just been completed recently. It is the editor used for comments and comments on this blog. Many browser compatibility problems encountered during Javascript writing are recorded here for reference.
When writing JavaScript, try to use methods and attributes in the ecmascript standard.
1. Obtain the selected text in the text box:
If (Document. Selection && Document. selection. Type = " Text " ) // IE
{
Selectedstr = Document. selection. createRange (). text;
}
Else If (Window. getselection && This . Textbox. selectionstart > - 1 ) // FF
{
VaR St = This . Textbox. selectionstart;
VaR Ed = This . Textbox. selectionend;
Selectedstr = This . Textbox. value. substring (St, Ed );
}
2. Capture events:
Menufontsize. onclick=Function (E)
{
VaR EVT=(Window.Event |E );
...
3. Stop event bubbling:
F(EVT. preventdefault)//IE
{
EVT. preventdefault ();
EVT. stoppropagation ();
}
Else //FF
{
EVT. cancelbubble= True;
EVT. returnvalue= False;
}
4. Registration event:
If(Document. attachevent)//IE
{
Document. attachevent ("Onclick", Hideeve );
}
ElseFF
{
Document. addeventlistener ("Click", Hideeve,False);
}
5. Get the value of the element in IFRAME:
Document. getelementbyid ("Zxjayubbeditor"Registrant .contentcontactdoc ument. getelementbyid ('Txt'). Value;
6. Get the text in the DIV element:
IE supports innertext
FF supports textcontent, but the textcontent of FF can be obtained with spaces and line breaks, and deleted with regular expressions.
$ (Objid). innertext//IE
$ (Objid). textcontent. Replace (/\\/G,""). Replace (/(^ \ N +) | (\ n + $)/G,"")//FF
7. Use the object-oriented method to write JavascriptProgramClear thinking and RedundancyCodeLess, it is worth advocating.
Supplement!
The method for obtaining the element value in IFRAME in the background is as follows:
When submitting a form, execute JavaScript code, use JavaScript to obtain the value of the element in IFRAME, and write it to the hidden control. You can simply request it in the background program.
To prevent submission failure and loss of written information, execute the onload () event during IFRAME loading and write the hidden control value to the IFRAME control.
Because there are still bugs that have not been solved and cannot be open-source, the source code will be released after correction.
This article is not written due to time reasons. Demo address: go down to the details page.