Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts
How to catch JavaScript Errors with window.onerror


I’m working on a new responsive website for company that has a fair amount of JavaScript and  that is viewed in lots of different browsers (mobile, tablet, desktop). 
Naturally we want to log our JavaScript exceptions and their stacktraces,  just like we log server-side exceptions. 
It is impossible to test every combination of device and browser so we rely on logging to find the edge cases we miss in our testing.

The way we handle our JavaScript exceptions is to:

 catch the exception.
 collect data about the useragent.

 Save it to our logs by sending an ajax request with the data and the exception information.


$(window).error(function (msg, url, line) 
{   
errorlog(msg.originalEvent.error);
    });



function errorlog(e) { if (typeof (e) == "object") { e.searchurl = location.href; } else if (typeof (e) == "string") { e += "SearchUrl" + location.href; } $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: location.protocol + "//" + location.hostname + "/" + location.pathname + "/errorlog", data: JSON.stringify({ _jsexception: e.stack }), dataType: "json", async: true, success: function (data, textStatus) { if (textStatus == "success") { if (data.hasOwnProperty("d")) { msg = data.d; } else { msg = data; } } }, error: function (data, status, error) { } }); }



Code behind
---------------------------------------------
[System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static void errorlog(object _jsexception)
        {
            string strEmailBody = string.Empty;

            if (_jsexception.GetType().Name == "String")
            {
                strEmailBody += "
Method Name:- Error " + _jsexception as string;

            }
            else
            {
                Dictionary dicErrorAttribute = _jsexception as Dictionary;
                foreach (var _keyandvalue in dicErrorAttribute)
                {
                    if (_keyandvalue.Key == "name")
                    {
                        strEmailBody += "
Method Name:- " + _keyandvalue.Value;

                    }
                    else
                    {
                        strEmailBody += "
" + _keyandvalue.Key + ":- " + _keyandvalue.Value;

                    }
                }
            }
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString);
            using (con)
            {
                SqlCommand cmd = new SqlCommand("proc_Error", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@Subject", "Error On JavaScript"));
                cmd.Parameters.Add(new SqlParameter("@Body", strEmailBody));
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
            }

        }






What are callbacks?

A callback is just a way to store some things to do at a later time.

A callback function is called at the completion of a given task.

The callback takes two arguments, resolve and reject , which are both functions. All your asynchronous code goes inside that callback. If everything is successful, the promise is fulfilled by calling resolve() . In case of an error, reject() is called with an Error object.


A promise is an object which can be returned synchronously from an asynchronous function. It will be in one of 3 possible states:
Fulfilled: onFulfilled() will be called (e.g., resolve() was called)
Rejected: onRejected() will be called (e.g., reject() was called)
Pending: not yet fulfilled or rejected
Step 1:

 <div>
           <label for="tags">Tags: </label>
            <input id="tags" />
 </div>

Step 2:

<script>

var sourcedata = [
    "asp.net",
    "Asp",
    "BASIC",
    "C",
    "C++",  
    "COBOL",
    "Fortran",
    "Java",
    "JavaScript",
    "Lisp",
    "Perl",
    "PHP",
    "Python",
    "Ruby",
    "Scala",
    "Scheme"
    ];
$("#tags").autocomplete({
    source: sourcedata,
    open: function (event, ui) {

            var adddiv = $("<div style=\"width: auto; background: #f5f5f5;border-top: 1px solid #c7c7c7;margin-top: 0.92307692em; padding: 0 0.46153846em;overflow: hidden;\"> Close</div>", {

            });
        adddiv.click(function () {
            $(event.target).autocomplete("close");
        });
        $(this).autocomplete("widget").append(adddiv);
    }  
   
});

</script>

Page refresh

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="Scripts/jquery-2.1.1.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            setTimeout(function () {
                window.location.reload();
            }, 60 * 1 * 1000);
           
            //Page refresh after 40 min -
            //Example :- 60 * 40 * 1000
            //Format:-  60 * (minute) * 1000
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
       <h1>Page refresh after 40 min </h1>
       <h2>
       Refresh Page 40 minute </h2>
       or
       <h3>Refresh Page for interval</h3>
       or
       <h4>Automatically reload a web page at a certain time</h4>
        <br />
       Or you can set value to refresh page
    </div>
    </form>
</body>
</html>


remove only html tags in a string 


var s = '<span>javascript</span> programming language'; 
var r = /<(\w+)[^>]*>.*<\/\1>/gi;
alert(s.replace(r,""))
Previous PostOlder Posts Home