(程式碼生成)JS寫的程式碼生成器-編碼也可以這麼爽..

G球發表於2007-09-27

以下程式碼儲存為HTML格式即可.
敬告:引用請註明出處

<script language = "javascript">
/*
Autor:Kangco
Date:2007/09/27
*/
function OutFile()
{
var text = "sdfsd";
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition",
"attachment;" + " filename="+pid+".html");
response.getOutputStream().write(text.getBytes());
response.getOutputStream().flush();
}

function SaveAsOk()
{
 document.execCommand("saveAs");
}

function WriteFile()
{
  var   fso   =   new   ActiveXObject("Scripting.FileSystemObject");  
  var   a   =   fso.CreateTextFile("c://testfile.txt",   true);  
  a.WriteLine("This   is   a   test.");  
  a.Close();  
  alert("我已經在你的C盤根目錄下生成了一個testfile.txt的檔案!");  
}

function Split_rn()
{
//alert(split);
//substring("asdfs")
alert(document.getElementById("A005").value);
alert(document.getElementById("A005").value.split("/r/n")[0]);
alert(document.getElementById("A005").value.split("/r/n")[1]);
}

function docWrite_rn()
{
//alert(split);
//substring("asdfs")
var o_OK_value = document.getElementById("A005").value;
document.write("sdf");
document.write("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+o_OK_value.split("/r/n")[0]+ "<br>");
document.write(o_OK_value.split("/r/n")[1]+ "<br>");
}

function JS_1()
{
 var o_split = document.getElementById("A005").value.split("/r/n");
 for (var i = 0; i < o_split.length;i++)
 {
  document.write("document.getElementById(/""+o_split[i]+"/").style.display=/"none/";");
  document.write("<br>");
 }
}

function JS_2()
{
 
 var o_split = document.getElementById("A005").value.split("/r/n");
 var o_A006_value = ReplaceAll(ReplaceAll(document.getElementById("A006").value,"/r/n","<br>")," ","&nbsp;");
 document.write("<a href='javascript:history.go(-1);'>返回</a><br>");
 for (var i = 0; i < o_split.length;i++)
 {
  //document.write("document.getElementById(/""+o_split[i]+"/").style.display=/"none/";");
  //document.write("<br>");
  //alert(o_split[i].split(",").length);
  var out_value = o_A006_value;//每次進行迴圈替換前均初始化為介面設定值
  
  //智慧判斷是否產生,以免產生垃圾程式碼
  var b_OK = "N";
  for(var j = 0; j < o_split[i].split(",").length;j++)
  {
   if(o_split[i].split(",")[j].trim() != ""&&o_split[i].split(",")[j].trim() != null)
   {
    b_OK = "Y";//其中一個不為空,即為有效產出
   }
  }
  //alert(b_OK );
  if(b_OK == "Y")
  {
   for(var j = 0; j < o_split[i].split(",").length;j++)
   {
    out_value = ReplaceAll(out_value,"{"+j+"}",o_split[i].split(",")[j].trim());
   }

   document.write("<div style = 'font-size:12px'>"+out_value+"</div>");
  }
 }

 
}
//Replace進一步擴充套件
function ReplaceAll(strOrg,strFind,strReplace)
{//eg:ReplaceAll(o_txtUNID_PRICE.value,",","")
    var index = 0;
    while(strOrg.indexOf(strFind,index) != -1)
    {
        strOrg = strOrg.replace(strFind,strReplace);
        index = strOrg.indexOf(strFind,index);
    }
    return strOrg
}

function JS_3()
{
 var o_AutoCode = document.getElementById("AutoCode");
 var o_TempCode = document.getElementById("TempCode");
 if(o_AutoCode.style.display == "")
 {
  o_AutoCode.style.display = "none";
  o_TempCode.style.display = "";
  document.getElementById("temp_eg").value = "產出設定";
  document.getElementById("006").disabled = true;
 }
 else
 {
  o_AutoCode.style.display = "";
  o_TempCode.style.display = "none";
  document.getElementById("temp_eg").value = "示例模板";
  document.getElementById("006").disabled = false;
 }
}
String.prototype.trim = function()
{
 var i,j;
 var str = this.replace(/(^/s*)|(/s*$)/g,'');
    if(str == "") return "";
    for(i=0;i<str.length;i++)
    if(str.charAt(i) != ' ') break;
    if(i >= str.length) return "";
    for(j=str.length-1;j>=0;j--)
    if(str.charAt(j) != ' ') break;
    return str.substring(i,j+1).replace(/(^/s*)|(/s*$)/g,''); 
}

</script>


<input id="001" type="hidden" value="button" οnclick="OutFile()"/>
<input id="002" type="hidden" value="button" οnclick="SaveAsOk()"/>
<input id="003" type="hidden" value="WriteFile" οnclick="WriteFile()"/>

<input id="004" type="hidden" value="Split" οnclick="Split_rn()"/>
<input id="005" type="hidden" value="docWrite" οnclick="docWrite_rn()"/>
<br>
<input id="006"  type="button" value="產生程式碼" οnclick="JS_2()"/>
<input id="temp_eg"  type="button" value="示例模板" οnclick="JS_3()"/>
<br>
<table id = "AutoCode">
<tr>
<td>引數</td>
<td>
<textarea id="A005" type="text" style="width:600px;height:200px;">
txtCONVERT_DATE,折舊日期</textarea>
</td>
</tr>
<tr>
<td>
模板
</td>
<td>
<textarea id="A006" type="text" style="width:800px;height:200px;">
document.getElementById("img{0}").style.display="none";//{1}
var o_{0}=document.getElementById("{0}");
if(o_{0}.value.trim()=='')
{//{1}
        document.getElementById("img{0}").style.display="";
        ShowError('00018','{1}');
        o_{0}.focus();
        return false;
}
</textarea>
</td>
</tr>
</table>
<table id = "TempCode" style = "display:none;font-size:12px;width:100%" cellspacing="0" cellpadding="0" border = "1">
<tr>
<td nowrap>
[JS]設定影像顯示為none
</td>
<td>
<textarea type="text" style="width:100%">
document.getElementById("img{0}").style.display="none";//{1}</textarea>
</td>
</tr>
<tr>
<td>
[JS]IsMust檢測
</td>
<td>
<textarea type="text" style="width:100%;height:200px">
var o_{0}=document.getElementById("txtDEPOSIT_ID");
if(o_{0}.value.trim()=='')
{//{1}
        document.getElementById("img{0}").style.display="";
        ShowError('00018','{1}');
        o_{0}.focus();
        return false;
}</textarea>
</td>
</tr>

</table>
 

相關文章