JavaScript 可扩展的代码,并根据从服务器返回的错误甚至是静态方式,自动调整自定义消息.... JSON

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 可扩展的代码,并根据从服务器返回的错误甚至是静态方式,自动调整自定义消息.... JSON相关的知识,希望对你有一定的参考价值。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Json</title>
    <style>
        html, body
        {
            margin: 0;
            padding: 0px;
            font-family: Arial, Helvetica, sans-serif;
            height: 100%;
            width: 100%;
            background: #99C8E8;
        }
        
        h1
        {
            font-size: 24px;
            padding: 0;
            margin: 0 0 8px 0;
        }
        
        #logo
        {
            width: 550px;
            margin: 50px auto 20px auto;
            text-align: center;
            padding-left: 112px;
        }
        
        #ErrorMessage
        {
            width: 100%;
            min-width: 650px;
            height: 100%;
        }
        
        #ErrorMessageContent
        {
           
            width: 550px;
            margin: 0 auto;
            font-size: 22px;
            border: #FFF 1px solid;
            color: #FFF;
            padding: 30px 50px 30px 50px;
        }
        
        #ErrorMessage h1
        {
            text-align: center;
        }
        .spanner
        {
            border:1px;
            display:block;
            background:#9CCBEF;
            padding:10px; 
        }
        .hmessage
        {
            color:#424542;
            font-size:18px;
            font-weight:bold;
            text-align:left;
            padding-right:300px;
        }
        .message_box
        {
            border:1px solid;
            padding:8px;
        }
        .pmessage
        {
            text-align:left;
            color:#717171;
            font-size:16px;
        } 
        #ErrorMessage p
        {
            padding: 0;
            margin: 0;
            line-height: 18px;
            font-size:12px;
            text-align: left;
            padding-top:5px;
        }
        
        #ErrorMessage p a, #ErrorMessage p a:link
        {
            display: block;
            color: #c5dae8;
            text-decoration: none;
        }
        
        #ErrorMessage p a:hover
        {
            color: #99C8E8;
        }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
    

</head>
<body>
    <div id="logo">
        <img src="images/yourimg.png" border="0" />
    </div>
    <div id="ErrorMessage">
        <div id="ErrorMessageContent">
           <div class="message_box">
           <div class="spanner"> 
                <font class="hmessage">Welcome to the site</font>
            
                <p class="pmessage">
                    We are currently performing planned maintenance.</p>
                <p>
                     </p>
            </div>
            </div>
           
        </div>
    </div>
</body>
</html>
<script type="text/javascript">


    var errorSettings = {

        getURLParameter: function (name) {

            var str = decodeURIComponent((RegExp('[?|&]' + name + '=' + '(.+?)(&|#|;|$)').exec(location.search) || [, ""])[1].replace(/\+/g, '%20')) || null;
            return str;
        },

        errorcode: function () { return errorSettings.getURLParameter("errorReason") },

        language: function () { return errorSettings.getURLParameter("lang") },

        hmessageDivId: {
            "0": "Access Denied",
            "1": "Access Denied",
            "2": "Access Denied",
            "3": "Access Denied",
            "4": "Access Denied",
            "5": "Access Denied",
            "6": "Access Denied",
            "7": "Access Denied",
            "8": "Access Denied",
            "9": "Access Denied"
        },

        messages: {
            "0": "Your user permissions do not authorize you to access this page.",
            "1": "The user name and password combination you entered does not correspond to a registered user.",
            "2": "The password you have entered is invalid.",
            "3": "Your account has been locked. Please contact your System Administrator.",
            "4": "Your account has been deleted. For further details, please contact your System Administrator.",
            "5": "Your account has expired. Please contact your System Administrator.",
            "6": "Invalid assertion.",
            "7": "Please provide a user name and a password.",
            "8": "You cannot access this product since you belong to no group. Please contact your System Administrator.",
            "9": "Invalid answer to secret question.",
            "10": "Your account has been deactivated. Please contact your System Administrator to reactivate it.",
            "11": "Your password is expired"
        },

        errorDivId: ".pmessage",
        
        hmessid: ".hmessage"
    };

    
    $(document).ready(function (e) {

        if (errorSettings.errorcode() != null) {
            // alert(errorSettings.messages[errorSettings.errorcode] + " :: " + errorSettings.language);
            $(errorSettings.hmessid).html(  errorSettings.hmessageDivId[errorSettings.errorcode()] )
            $(errorSettings.errorDivId).html('<p><font class="sublabel">' + errorSettings.messages[errorSettings.errorcode()] + '</font></p>')

        }

    });
       
</script>

以上是关于JavaScript 可扩展的代码,并根据从服务器返回的错误甚至是静态方式,自动调整自定义消息.... JSON的主要内容,如果未能解决你的问题,请参考以下文章

如何从JavaScript中引用属于Chrome扩展程序的文件

在 iOS 共享扩展中使用动态 Javascript

Browserify

书写规范的javaScript

扩展 Javascript 承诺并在构造函数中解决或拒绝它

将浏览器扩展转换为常规网站