简体   繁体   English

如果字符串来自javascript编码,我如何解码html文本框的字符串

[英]how can i decode a String for html text box if the string is coming from javascript with encoding

we are working on a project that uses struts 2 ,JSP and for validations we are using JavaScript coming to my problem , in some cases we are setting text box values from JavaScript on page on Load() method . 我们正在开发一个使用struts 2,JSP的项目,为了进行验证,我们使用了JavaScript来解决我的问题,在某些情况下,我们在Load()方法页面的JavaScript中设置了文本框值。 in that we are encoding string in order to avoid script errors and next we are setting that value to text box . 为了避免脚本错误,我们对字符串进行编码,然后我们将该值设置为文本框。 but I want to decode that text box value before its setting to text box and then after decoding it we need to display in text box . 但是我想在将该文本框值设置为文本框之前对其进行解码,然后在对其进行解码之后,我们需要在文本框中显示该值。 thanks in advance. 提前致谢。
and the code is: 代码是:

   <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html  xmlns="http://www.w3.org/1999/xhtml">








<head>
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dijit/themes/claro/claro.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/resources/Grid.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/resources/tundraGrid.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/enhanced/resources/claro/EnhancedGrid.css"/>
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/grid/enhanced/resources/EnhancedGrid_rtl.css"/>
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/widget/ColorPicker/ColorPicker.css" />
    <link rel="stylesheet" type="text/css" href="/cxp/scripts/dojo 1.6/dojox/form/resources/FileInput.css" />

    <script type="text/javascript"  djConfig="parseOnLoad: true, isDebug: false" src="/cxp/scripts/dojo 1.6/dojo/dojo.js"></script>
    <script type='text/javascript' src='/cxp/scripts/jquery/libs/jquery-1.4.4.min.js'></script>
    <script type='text/javascript' src='/cxp/scripts/jquery/libs/jquery-ui-1.8.11.custom.min.js'></script>
    <script type="text/javascript" src="/cxp/scripts/jquery/libs/date.js"></script>
    <script type='text/javascript' src='/cxp/scripts/jquery/jquery.weekcalendar.js'></script>
    <script type='text/javascript' src='/cxp/scripts/jquery/jquery-dateformat.js'></script>

    <link rel='stylesheet' type='text/css' href='/cxp/scripts/jquery/libs/css/smoothness/jquery-ui-1.8.11.custom.css' />
    <link rel="stylesheet" type="text/css" href="/cxp/styles/styles.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/styles/wareHouseStyles.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/styles/maintenance.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/styles/dojostyles.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/styles/supervisor.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/styles/warehouse.css"></link>
    <link rel="stylesheet" type="text/css" href="/cxp/styles/alignment.css"></link>

    <script type="text/javascript"  src="/cxp/scripts/js/popup.js"></script>
    <script type="text/javascript"  src="/cxp/scripts/js/date.js"></script>
    <script type="text/javascript"  src="/cxp/scripts/js/validations.js"></script>
    <script type="text/javascript"  src="/cxp/scripts/js/dojovalidations.js"></script>
    <script type="text/javascript"  src="/cxp/scripts/js/reports.js"></script>  
    <script type="text/javascript"  src="/cxp/scripts/js/formateDate.js"></script>
    <script type="text/javascript"  src="/cxp/scripts/jspspellcheck/spellcheck-caller.js"></script>
</head>
<head>
<title>CaterXpert - Maintenance</title>

<script type="text/javascript">
    dojo.require("dijit.form.Form");
    dojo.require("dijit.form.TextBox");
    dojo.require("dijit.form.Button");
    dojo.require("dijit.form.RadioButton");
    dojo.require("dijit.form.Select");
    dojo.require("dojo.parser");
     var res;
    dojo.addOnLoad (function() { 
         var id="54";
        /*  var id1="lookupBean [Type=""#***^&&&'''#$^%''&$#@"L:"?>aa, lookupId=54, Status=null, SortOrder=1, Active=1, Days=null, category=null, name=null, unitTypeId=null, invSupplierId=null, beverageCategoryID=null, cost=null, price=null, description=null, unitId=null, caterDocsId=null, sOrder=0, lookUpActive=null, taxType=null]"; */
         if(id >0 && id !=""){

                                dojo.byId("Type1").value=decodeTags("&#034;&#034;#***^&amp;&amp;&amp;&#039;&#039;&#039;#$^%&#039;&#039;&amp;$#@&#034;L:&#034;?&gt;aa‌​");

                               dojo.byId("SortOrder2").value="1";
                }
         if(id > 0 && id !=""){
            var active  =   '1';
            if(active ==1){
                dijit.byId("active").setValue(true);
            }
            else if(status ==0){
                dijit.byId("inactive").setValue(true);
            }
         }
          if (document.pub) {
              document.pub();
          } 

       });  

    function decodeTags(str) {
        return str
             .replace(/&/g, "&amp;")
             .replace(/</g, "&lt;")
             .replace(/>/g, "&gt;")
             .replace(/"/g, "&quot;")
             .replace(/'/g, "&#039;");
             // add whatever you want to replace.
     }
      function submitForm(){
       if(validateAll()){
           var type =  dojo.byId("type").value;

                 document.getElementById("lookupId").value=1;

           document.forms[0].action = "saveSystemSettingLookup.action?type="+type;
           document.forms[0].submit();
        }
     }

      function validateAll(){ 
         var temp=true;


                            if(dojo.byId("Type1").value==""){
                            alert("Empty Type");
                            temp=false;
                              }
                            if(res>0){
                                alert('DUPLICATE TYPE TRY AGAIN');
                                temp=false;
                            }


                   var sortOrder= dojo.byId("SortOrder2").value;
                   if(sortOrder !=""){
                      var onlyNums = /[^0-9]/g;
                               if (onlyNums.test(sortOrder)) { 
                                   alert(" Invalid Sort Order"); 
                                  temp= false;
                                }
                      }



            return temp;
     }

    function doClose()
    {
         var lookUpId  =  dojo.byId("id").value;
         var type  =  dojo.byId("type").value;
           if(lookUpId > 0){
               parent.view.location.replace("/cxp/corpmaintenance/viewSystemSettingLookup.action?type="+type);

            }
            else{
                 window.parent.location="/cxp/corpmaintenance/viewCorpMaintenanceFrame.action";
         }
    }

    function duplicateChecking()
    {
        res = 0;
         var type = dojo.byId("type").value;
         var id   = document.getElementById("id").value;
         var columnName=document.getElementById("columnName").value;
         var idValue1=document.getElementById("idValue").value;
         var name = document.getElementById(idValue1).value;
         /* This code is for Ajax*/
        var xmlhttp;
        if (window.XMLHttpRequest)
          {
          // code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else if (window.ActiveXObject)
          {
          // code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        else
          {
          alert("Your browser does not support XMLHTTP!");
          }
        xmlhttp.onreadystatechange=function()
        {
            if(xmlhttp.readyState==4)
              {
                res = eval( "("+ xmlhttp.responseText +")" );
                submitForm();

              }
        }
        xmlhttp.open("GET","/cxp/corpmaintenance/checkSystemSettingLookup.action?columnName="+columnName+"&name="+name+"&type="+type+"&id="+id+"&rand="+Math.random(),true);
        xmlhttp.send(null);
    } 

</script>
</head>
<body class="claro">
<div>
<form  name="bean" action="" id="bean">
  <input  name="id"  id="id" type="hidden" value="54" />
  <input  name="type" id="type" type="hidden" value="DeliveryType" />
  <input type="hidden" name="lookupId"  id ="lookupId"/>
  <input type="hidden" name="formName" id="formName" value=""></input>

            <table class="formContentPane column-width35" align="center">
             <tr><td>&nbsp;</td></tr>

                    <tr>  
                                    <td class="paddingleftfive" style="width: 5%" nowrap="nowrap"><label>Type</label></td>
                                    <td class="mandatoryField" style="width: 3px">
                                    </td>

                        <td >       
                                            <input dojoType="dijit.form.TextBox"  id="Type1"  name="bean.Type" size="0" maxlength="50"/>

                                        <input type="hidden" id="columnName" name="columnName" value="Type"></input>
                                            <input type="hidden" id="idValue" name="idValue" value="Type1"></input>

                        </td>
                    </tr>

                    <tr>  
                                    <td class="paddingleftfive" style="width: 5%" nowrap="nowrap"><label>SortOrder</label>
                                    </td>
                                    <td>&nbsp;</td>


                        <td >    
                                        <input dojoType="dijit.form.TextBox"  id="SortOrder2"  
                                        name="bean.SortOrder" size="0" maxlength="5"/>


                                        <input type="hidden" id="columnName" name="columnName" value="SortOrder"></input>
                                            <input type="hidden" id="idValue" name="idValue" value="SortOrder2"></input>

                        </td>
                    </tr>
                    <tr>
                        <td style="width: 5%" nowrap="nowrap" class="paddingleftfive"><label>Active</label></td>
                        <td>&nbsp;</td>
                        <td>
                            <input  dojoType="dijit.form.RadioButton" checked="checked" id="active" name="bean.active" type="radio" value="1"/>
                            <label>Yes</label>
                            <input  dojoType="dijit.form.RadioButton" name="bean.active" id="inactive" type="radio" value="0"/>
                            <label>No</label>
                        </td>
                    </tr>
                    <tr></tr>

             <tr><td>&nbsp;</td></tr>
       </table>

            <table class="saveclosebuttons" align="center" >
                <tr>
                    <td align="center">

                                <button dojoType="dijit.form.Button" onclick="duplicateChecking()" class="mediumbutton" type="button">
                                Save</button>

                            <button dojoType="dijit.form.Button" onclick="doClose()" class="mediumbutton" type="button">
                            Close</button>
                    </td>
                </tr>
        </table>
        </form>
</div>
</body>
</html>

Not sure if you want to convert < to & lt; 不确定是否要将<转换为&lt; or vise versa, but you can write a function in javascript to decode all the unwanted characters. 反之亦然,但是您可以使用JavaScript编写函数以解码所有不需要的字符。

function decodeTags(str) {
    return str
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
         // add whatever you want to replace.
 }

Editing my original answer. 编辑我的原始答案。 Thanks for all the details and the detailed html code. 感谢您提供所有详细信息和详细的html代码。 It looks like we need to do the reverse of what we did. 看来我们需要做的与我们相反。

function decodeTags(str) {
    return str
         .replace(/&#034;/g, "\"")
         .replace(/&amp;/g, "&")
         .replace(/&#039;/g, "'")
         .replace(/&gt;/g, ">")
         .replace(/&lt;/g, "<");
         // add whatever you want to replace.
 }

You can do encoding and decoding like this : 您可以像这样进行编码和解码:

function encode(value){
  return $('<div/>').text(value).html();
}

function decode(value){
  return $('<div/>').html(value).text();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM