最近的一些小技巧

来源:互联网 发布:oracle数据库面试宝典 编辑:程序博客网 时间:2024/05/14 23:56

asp计算中英文字符长度

function strlen(str)
dim p_len
p_len=0
strlen=0
if trim(str)<>"" then
p_len=len(trim(str))
for xx=1 to p_len
if asc(mid(str,xx,1))<0 then
strlen=int(strlen) + 2
else
strlen=int(strlen) + 1
end if
next  

 

javascript

function calculate_byte( sTargetStr ) {
var sTmpStr, sTmpChar;
var nOriginLen = 0;
var nStrLength = 0;
sTmpStr = new String(sTargetStr);
nOriginLen = sTmpStr.length;
for ( var i=0 ; i < nOriginLen ; i++ ) {
sTmpChar = sTmpStr.charAt(i);
if (escape(sTmpChar).length > 4) {
nStrLength += 2;
} else if (sTmpChar!='/r') {
nStrLength ++;
}
}
return nStrLength;
}

----------------------------------------------------------------------------

一般思维都是先确定该字符是不是双字节,如果是长度+2,否则加1

确定是双字节的方法有,指定是双字节的unicode范围表达式,2)编码后是asc码,3)统一编成, 确定期长度,借助function的特性,

htmlEnCode将特殊字符用unicode字符代替。也可server.htmlEncoder来做

function jsInject(content)
if content<>"" then
content= Replace(content, "/", "&#92;")
content= Replace(content, "/", "&#47;")
content= Replace(content, "<", "&#60;")
content= Replace(content, ">", "&#62;")
content = Replace(content, CHR(32), "&nbsp;")
content = Replace(content, CHR(9), "&nbsp;")
content = Replace(content, CHR(34), "&quot;")
content = Replace(content, CHR(39), "&#39;")
content = Replace(content, CHR(13), "")
content = Replace(content, CHR(10) & CHR(10), "<p></p> ")
content = Replace(content, CHR(10), "<br> ")
end if
jsInject= content
end function

function cut_str(content, content_len) '长度,记得一般要-3, 让对齐。但中english难区分。。。
 if len(content)>content_len then
  cut_str = Left(content, (content_len-3))&"..."
 else
  cut_str = content
 end if
end function

---------------------------------------------------------

function myFormateDate(orgDate, para)
myYear= Year(orgDate)
myMonth= Month(orgDate)
myDay=Day(orgDate)
myHour= Hour(orgDate)
myMinute= Minute(orgDate)
mySecond= Second(orgDate)
Select Case para
case "1"
myFormateDate= RIGHT("0000"&myYear,4)&"-"&RIGHT("00"&myMonth,2)&"-"&RIGHT("00"&myDay,2)
case "2"
myFormateDate= RIGHT("00"&myMonth,2)&"-"&RIGHT("00"&myDay,2)&" "&RIGHT("00"&myHour,2)&":"&RIGHT("00"&myMinute,2)
case else
myFormateDate= RIGHT("0000"&myMonth,4)&"-"&RIGHT("00"&myMonth,2)&"-"&RIGHT("00"&myDay,2)
end Select
end function

-----------------------------------------------------------------

创建高度动态变化的Iframe

iframe,尤其是不带边框的iframe因为能和网页无缝的结合从而不刷新页面的情况下更新页面的部分数据成为可能,可是iframe的大小却不像层那样可以“伸缩自如”,所以带来了使用上的麻烦,给iframe设置高度的时候多了也不好,少了更是不行,现在,我终于知道了让iframe动态体调整高度的方法,主要是以下JS函数:
function SetCwinHeight()
{
var cwin=document.getElementById("cwin";
if (document.getElementById)
{
if (cwin && !window.opera)
{
if (cwin.contentDocument && cwin.contentDocument.body.offsetHeight)
cwin.height = cwin.contentDocument.body.offsetHeight;
else if(cwin.Document && cwin.Document.body.scrollHeight)
cwin.height = cwin.Document.body.scrollHeight;
}
}
}

最后,加入iframe,不能丢掉onload属性,当然了,id也必须也函数中的cwin匹配
<iframe width="778" align="center" height="200" id="cwin" name="cwin" onload="Javascript:SetCwinHeight()" frameborder="0" scrolling="no"></iframe>

这样使用效果确实不错

这里效果主要是在iFrame加载完后再跟据onload="height=body.scrollHeight"

------------------------------------------------

原创粉丝点击