基於ASP的Web開發中常用代碼

來源:互聯網
上載者:User

<!--使表格帶有串連功能--->
<html>
<head>
<title>HTML document</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="200" height="200" bgcolor="Gray"><tr><td onmouseover=this.bgColor="#800000" onclick=window.location.href="http://www.google.com">google</td></tr><tr><td onmouseover=this.bgColor="#008000" onclick=window.location.href="http://www.blueidea.com">bluidea.c ... 0" onclick=window.location.href="http://www.blueidea.com/bbs">bbs</td></tr></table>
</body>
</html>

<%
'擷取URL值,跳轉到新網址
dim a,b
a=request.querystring("boardID")
b=request.querystring("id")
response.redirect("http://bbs.anquan.com.cn/dispbbs.asp?boardid ... "&id="&b)
%>

<%@ Language=VBScript %>
<html>
<title>No.2根據遠程主機地址來進行判斷,如果為本地地址則進入歡迎頁面,否則顯示出錯資訊.</title>
<body>

本地ip
<%=Request.ServerVariables("REMOTE_ADDR")%>
伺服器IP
<%=Request.ServerVariables("LOCAL_ADDR")%>
<%
dim address
address = request.servervariables("REMOTE_ADDR")
response.write address
if address="127.0.0.1" then
'如果為本地,則顯示歡迎頁面.
response.write "你好,歡迎進入本網站."
'response.redirect "http://localhost/100asp/001.asp"
else
'否則顯示出錯資訊.
response.write "對不起,你無權查看內部網站."
end if
%>
</body>
</html>

'按時間判斷,顯示最新標誌
<%
mytime=rs("time") '資料庫中儲存時間
nowtime=now() '目前時間
if datediff("d",mytime,nowtime)<1 then
'比較目前時間和資料庫中時間,如相差小於1,即顯示圖片new.gif
%>
<img src="images/new.gif">
<%end if%>

'圖片大時,自動縮小的寫法.
<img border="0" src="<%=trs("e_simg")%>" onload="javascript:if(this.width>120)this.width=120" alt="<%=trs("e_trem")%>">

'幾個條件陳述式的寫法
while ...........wend
do while .................loop
有判斷是
if........else end if
if........elseif end if

'長標題截取的寫法1
<% if len(rs("showname"))>13 then%>
<%=left(rs("showname"),12)%>..
<% else%>
<%=rs("showname")%>
<%end if%>
'寫法2
<%if len(rs("quizTitle"))>13 then response.write left(rs("quizTitle"),12)&".." else response.write rs("quizTitle") end if%>

'出錯或完成操作後提示句.
<%
response.write "<script language=javascript>alert('請留言!');window.location.href='liuyan.asp';</script>"
%>
<%
response.write "<script language=JavaScript>" & chr(13) & "alert('請檢查您填寫的內容是否完整!');" & "history.back()" & "</script>"
%>

'把表中有限的幾個分類轉換成"漢字"
<%
dim quizClass,quizOption
'quizClass=rs("quizLib.quizClass")
quizClass=rs("quizClass")
select case quizClass
case "radio1"
response.Write("判斷題")
case "radio2"
response.Write("單選題")
case "checkbox"
response.Write("多選題")
case "text"
response.Write("填空題")
end select
%>

'確定刪除一條記錄
<a href="del.asp?info_id=<%=rs("info_id")%>" onclick=return(confirm("確定要刪除第<%=rs("info_id")%>條記錄嗎?"))>刪除</a>

'動態顯示下拉式功能表
'如果記錄集為空白就指向首頁
'value值為表subject中的ID
<select name="booktype" class="button1" id="booktype" style="width:250">
<option value=all selected>試題學科</option>
<%
sql="SELECT * FROM subject"
set rs=conn.execute(sql)
if rs.bof or rs.eof then response.Redirect("index.asp")
while not(rs.bof or rs.eof)
response.Write "<option value="&rs("id")&">"&rs("subjName")&"</option>"
rs.movenext
wend
%>
</select>

'擷取資料庫裡的
<select name="mem_sex" id="mem_sex">
<option value="男" <%if rs("mem_sex")="男" then response.write "selected"%>>男</option>
<option value="女" <%if rs("mem_sex")="女" then response.write "selected"%>>女</option>
</select>
<!--限制文字框不能輸入空格-->
<script language="javascript">
function id_keyup(txtinput)
{
txtinput.value=txtinput.value.replace(/(^/s*)|(/s*$)/g, "");
}
</script>
<input type="text" name="id" onkeyup="id_keyup(this)">

9.隨機背景音樂:
<%randomize%>
<bgsound src="mids/<%=(int(rnd()*60)+1)%>.mid" loop="-1">

可以修改數字,限制調用個數,我這裡是60個.

狀態列文字的顯示,這個簡單實用!
<BASE onmouseover="window.status='歡迎光臨!-我的郵件:mxsky{at}126.com';return true">

開啟的一個迷你視窗

『複製代碼』『運行代碼』

<script language="javascript">
function mini_window(page,w,h)
//開啟一個小視窗
/*
page 頁面名稱
w 寬度
h  高度
*/
{
var arr = showModalDialog(page, "", "dialogWidth:"+w+"px; dialogHeight:"+h+"px; status:0;help:0");
}
//開啟一個小視窗
</script>
<body style="overflow:scroll;overflow-x:hidden;overflow-y:hidden">
<a style=cursor:hand ; onclick=mini_window('default.asp','650','515')>開啟新視窗</a>
</body>

加標題的電子郵件:
<a href="mailto:admin@anquan.com.cn?subject=關於考試系統的問題">flashboy</a>

iframe調用時如何讓背景透明
<Iframe src="transparent.htm" allowtransparency="true"></Iframe>
transparent.htm
<body style="background-color:transparent">

按鈕在原視窗開啟
<input type="button" name="button2" value="註冊" onclick="location.href='http://www.google.com'">

按鈕在新視窗開啟
<input type="button" name="button2" value="註冊" onclick="window.open('http://www.google.com')">

讓英文字串超出表格寬度自動換行
<td style="word-wrap:break-word; word-break:break-all;">

永遠都會帶著架構
<script language="JavaScript">
<!--
if (window == top)top.location.href = "frames.htm"; //frames.htm為框架頁
// -->
</script>

防止被人frame
<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
if (top.location != self.location)top.location=self.location;
// -->
</SCRIPT>

網頁將不能被另存新檔
<noscript><iframe src=*.html></iframe></noscript>

刪除時確認
<a href='javascript:if(confirm("確實要刪除嗎?"))location="del.asp"'>刪除</a>

不要捲軸
讓豎條沒有:
<body style='overflow:scroll;overflow-y:hidden'>
</body>
讓橫條沒有:
<body style='overflow:scroll;overflow-x:hidden'>
</body>
兩個都去掉?更簡單了:
<body scroll="no">
</body>

滑鼠划過,表格變色
<tr onmouseover="this.bgColor='red'" onmouseout="this.bgColor=''">

屏蔽右鍵
<body oncontextmenu="return false;">

<script language="JavaScript">
<!--
if (window.Event)
document.captureEvents(Event.MOUSEUP);
function nocontextmenu()
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
function norightclick(e)
{
if (window.Event)
{
if (e.which == 2 || e.which == 3)
return false;
}
else
if (event.button == 2 || event.button == 3)
{
event.cancelBubble = true
event.returnValue = false;
return false;
}
}
document.oncontextmenu = nocontextmenu; // for IE5+
document.onmousedown = norightclick; // for all others
//-->
</script>

用正則強制輸入數字
<input name="id2" type="text" onkeyup='this.value=this.value.replace(//D/gi,"")'>

禁止輸入中文
<input type=text style="ime-mode: disabled ">

不允許文字框自動完成
<input autocomplete=off>

圖片變灰,掠過恢複
<img src="http://community.csdn.net/images/csdn.gif" style="filter:gray" onmo ... useout="this.style.filter='gray'">

定時跳轉到其它頁面
<meta http-equiv="refresh" content="5;URL=http://54caizi.com">

TITLE換行
<a href=# title="第一行第二行第三行">title分行測試</a>

行背景色漸層
<table width="100%" height="100%">
<tr><td style="FILTER: progid:DXImageTransform.Microsoft.Gradient(endColorstr='#EFF3FF', startColorstr='#94B2F7', gradientType='1')" ></td></tr>
</table>
說明:
功能從startcolorstr漸層到endcolorstr
gardientType 是填充樣式,具體值請自己嘗試

調用其它頁面
<object type="text/x-scriptlet" width="800" height="1000" data="a.htm"></object>

Iframe的常用用法
<iframe WIDTH=100% HEIGHT=240 MARGINWIDTH=0 MARGINHEIGHT=0 HSPACE=0 VSPACE=0 FRAMEBORDER=0 allowTransparency="true" SCROLLING=no SRC='Cay/blog.asp'></iframe>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.