Experience writing the UBB Editor-compiling browser-compatible JavaScript code

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.