"Turn" JS debugging information Output Tips

Source: Internet
Author: User

Original address: http://blog.csdn.net/jom_ch/article/details/864574

Usually we do a simple JS debug output when used to use the alert function, this function can also solve most problems, but encountered such as cyclic output, scroll bar status monitoring, mouse position and other scenes, alert is quite inconvenient, the following function can solve the problem:

[JavaScript]View Plaincopy
  1. <script type="Text/javascript" >
  2. function __js_debug_msg () {
  3. var mydiv = document.createelement (' div ');
  4. Mydiv.id = "Js_debug_msg";
  5. Mydiv.setattribute (' style ',' position:fixed;width:300px;height:40px;padding:5px;background: #333;  Line-height:20px;color: #FFF; margin-top:0px;top:0px;right:0px; ');
  6. Mydiv.innerhtml="Print JS debug message.";
  7. Document.body.appendChild (MYDIV);
  8. }
  9. __js_debug_msg ();
  10. </script>

Put the above code at the bottom of the page where you need to output debugging information, and then use the innerHTML code alert when you need to output debugging information, such as:

[JavaScript]View Plaincopy
    1. document.getElementById (' js_debug_msg '). InnerHTML = ' information required for output ';

with Jqeury words:

[JavaScript]View Plaincopy
    1. $ (' #js_debug_msg '). HTML (' information that needs to be output ');

You can also output error messages

[JavaScript]View Plaincopy
    1. <script type="Text/javascript" >
    2. Need to be placed on the head of the page, such as the head area
    3. Window.onerror = function (err_msg, filename, line_num) {
    4. document.getElementById (' js_debug_msg '). InnerHTML = ' error message: ' +err_msg+' |line: ' +line_num+' |  File: ' +filename;
    5. return false;
    6. }
    7. </script>

"Turn" JS debugging information Output Tips

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.