using System;
using System.Web;
using System.Web.UI.HtmlControls ;
using System.Web.UI;

namespace Web.JScript

...{

/**//// <summary>
/// 提供向页面输出客户端代码实现特殊功能的方法
/// </summary>
/// <remarks>
/// </remarks>

public class JScript

...{
public static void AlertAndRedirect(string message,string toURL)

...{
string js = "<script language=javascript>alert('{0}');window.location.replace('{1}')</script>";
HttpContext.Current.Response.Write(string.Format(js,message ,toURL));
}


/**//// <summary>
/// 向客户端发送函数KendoPostBack(eventTarget, eventArgument)
/// 服务器端可接收__EVENTTARGET,__EVENTARGUMENT的值
/// </summary>
/// <param name="page">System.Web.UI.Page 一般为this</param>
public static void JscriptSender(System.Web.UI.Page page)

...{
page.RegisterHiddenField("__EVENTTARGET","");
page.RegisterHiddenField("__EVENTARGUMENT","");
string s = @"
<script language=Javascript>
function KendoPostBack(eventTarget, eventArgument)
{
var theform = document.forms[0];
theform.__EVENTTARGET.value = eventTarget;
theform.__EVENTARGUMENT.value = eventArgument;
theform.submit();
}
</script>";
page.RegisterStartupScript("sds",s);
}

/**//// <summary>
/// 弹出JavaScript小窗口
/// </summary>
/// <param name="js">窗口信息</param>
public static void Alert(string message)

...{
message = StringUtil.DeleteUnVisibleChar(message);
string js=@"<Script language='JavaScript'>
alert('"+ message +"');</Script>";
HttpContext.Current.Response.Write(js);
}
public static void Alert(object message)

...{
string js=@"<Script language='JavaScript'>
alert('{0}');
</Script>";
HttpContext.Current.Response.Write(string.Format(js,message.ToString()));
}

public static void RtnRltMsgbox(object message,string strWinCtrl)

...{
string js = @"<Script language='JavaScript'>
strWinCtrl = true;
strWinCtrl = if(!confirm('"+ message +"'))return false;</Script>";
HttpContext.Current.Response.Write(string.Format(js,message.ToString()));
}

/**//// <summary>
/// 回到历史页面
/// </summary>
/// <param name="value">-1/1</param>
public static void GoHistory(int value)

...{
string js=@"<Script language='JavaScript'>
history.go({0});
</Script>";
HttpContext.Current.Response.Write(string.Format(js,value));
}


/**//// <summary>
/// 关闭当前窗口
/// </summary>
public static void CloseWindow()

...{
string js=@"<Script language='JavaScript'>
window.close();
</Script>";
HttpContext.Current.Response.Write(js);
HttpContext.Current.Response.End();
}


/**//// <summary>
/// 刷新父窗口
/// </summary>
public static void RefreshParent()

...{
string js=@"<Script language='JavaScript'>
parent.location.reload();
</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 格式化为JS可解释的字符串
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
public static string JSStringFormat(string s)

...{
return s.Replace(" ","/r").Replace(" ","/n").Replace("'","/'").Replace(""","/"");
}


/**//// <summary>
/// 刷新打开窗口
/// </summary>
public static void RefreshOpener()

...{
string js=@"<Script language='JavaScript'>
opener.location.reload();
</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 打开窗口
/// </summary>
/// <param name="url"></param>
public static void OpenWebForm(string url)

...{

/**//*…………………………………………………………………………………………*/

/**//*修改目的: 新开页面去掉ie的菜单。。。 */

string js=@"<Script language='JavaScript'>
//window.open('"+url+@"');
window.open('"+url+@"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
</Script>";

/**//*结束*/

/**//*…………………………………………………………………………………………*/
HttpContext.Current.Response.Write(js);
}
public static void OpenWebForm(string url,string name,string future)

...{
string js=@"<Script language='JavaScript'>
window.open('"+url+@"','"+name+@"','"+future+@"')
</Script>";
HttpContext.Current.Response.Write(js);
}
public static void OpenWebForm(string url,string formName)

...{

/**//*…………………………………………………………………………………………*/

/**//*修改目的: 新开页面去掉ie的菜单。。。 */

/**//*注释内容: */

/**//*开始*/
string js=@"<Script language='JavaScript'>
window.open('"+url+@"','"+formName+@"','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');
</Script>";

/**//*结束*/

/**//*…………………………………………………………………………………………*/

HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:OpenWebForm
/// 功能描述:打开WEB窗口
/// </summary>
/// <param name="url">WEB窗口</param>
/// <param name="isFullScreen">是否全屏幕</param>
public static void OpenWebForm(string url,bool isFullScreen)

...{
string js=@"<Script language='JavaScript'>";
if(isFullScreen)

...{
js+="var iWidth = 0;";
js+="var iHeight = 0;";
js+="iWidth=window.screen.availWidth-10;";
js+="iHeight=window.screen.availHeight-50;";
js+="var szFeatures ='width=' + iWidth + ',height=' + iHeight + ',top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no';";
js+="window.open('"+url+@"','',szFeatures);";
}
else

...{
js+="window.open('"+url+@"','','height=0,width=0,top=0,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no,directories=no');";
}
js+="</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 转向Url制定的页面
/// </summary>
/// <param name="url"></param>
public static void JavaScriptLocationHref(string url)

...{
string js=@"<Script language='JavaScript'>
window.location.replace('{0}');
</Script>";
js=string.Format(js,url);
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 指定的框架页面转换
/// </summary>
/// <param name="FrameName"></param>
/// <param name="url"></param>
public static void JavaScriptFrameHref(string FrameName,string url)

...{
string js=@"<Script language='JavaScript'>
@obj.location.replace(""{0}"");
</Script>";
js = js.Replace("@obj",FrameName );
js=string.Format(js,url);
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
///重置页面
/// </summary>
public static void JavaScriptResetPage(string strRows)

...{
string js=@"<Script language='JavaScript'>
window.parent.CenterFrame.rows='"+strRows+"';</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:JavaScriptSetCookie
/// 功能描述:客户端方法设置Cookie
/// </summary>
/// <param name="strName">Cookie名</param>
/// <param name="strValue">Cookie值</param>
public static void JavaScriptSetCookie(string strName,string strValue)

...{
string js=@"<script language=Javascript>
var the_cookie = '"+strName+"="+strValue+@"'
var dateexpire = 'Tuesday, 01-Dec-2020 12:00:00 GMT';
//document.cookie = the_cookie;//写入Cookie<BR>} <BR>
document.cookie = the_cookie + '; expires='+dateexpire;
</script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:GotoParentWindow
/// 功能描述:返回父窗口
/// </summary>
/// <param name="parentWindowUrl">父窗口</param>
public static void GotoParentWindow(string parentWindowUrl)

...{
string js=@"<Script language='JavaScript'>
this.parent.location.replace('"+parentWindowUrl+"');</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:ReplaceParentWindow
/// 功能描述:替换父窗口
/// </summary>
/// <param name="parentWindowUrl">父窗口</param>
/// <param name="caption">窗口提示</param>
/// <param name="future">窗口特征参数</param>
public static void ReplaceParentWindow(string parentWindowUrl,string caption,string future)

...{
string js="";
if(future!=null&&future.Trim()!="")

...{
js=@"<script language=javascript>this.parent.location.replace('"+parentWindowUrl+"','"+caption+"','"+future+"');</script>";
}
else

...{
js=@"<script language=javascript>var iWidth = 0 ;var iHeight = 0 ;iWidth=window.screen.availWidth-10;iHeight=window.screen.availHeight-50;
var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';this.parent.location.replace('"+parentWindowUrl+"','"+caption+"',szFeatures);</script>";
}

HttpContext.Current.Response.Write(js);
}

/**//// <summary>
/// 函数名:ReplaceOpenerWindow
/// 功能描述:替换当前窗体的打开窗口
/// </summary>
/// <param name="openerWindowUrl">当前窗体的打开窗口</param>
public static void ReplaceOpenerWindow(string openerWindowUrl)

...{
string js=@"<Script language='JavaScript'>
window.opener.location.replace('"+openerWindowUrl+"');</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:ReplaceOpenerParentWindow
/// 功能描述:替换当前窗体的打开窗口的父窗口
/// </summary>
/// <param name="openerWindowUrl">当前窗体的打开窗口的父窗口</param>
public static void ReplaceOpenerParentFrame(string frameName,string frameWindowUrl)

...{
string js=@"<Script language='JavaScript'>
window.opener.parent." + frameName + ".location.replace('"+frameWindowUrl+"');</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:ReplaceOpenerParentWindow
/// 功能描述:替换当前窗体的打开窗口的父窗口
/// </summary>
/// <param name="openerWindowUrl">当前窗体的打开窗口的父窗口</param>
public static void ReplaceOpenerParentWindow(string openerParentWindowUrl)

...{
string js=@"<Script language='JavaScript'>
window.opener.parent.location.replace('"+openerParentWindowUrl+"');</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:CloseParentWindow
/// 功能描述:关闭窗口
/// </summary>
public static void CloseParentWindow()

...{
string js=@"<Script language='JavaScript'>
window.parent.close();
</Script>";
HttpContext.Current.Response.Write(js);
}
public static void CloseOpenerWindow()

...{
string js=@"<Script language='JavaScript'>
window.opener.close();
</Script>";
HttpContext.Current.Response.Write(js);
}


/**//// <summary>
/// 函数名:ShowModalDialogJavascript
/// 功能描述:返回打开模式窗口的脚本
/// </summary>
/// <param name="webFormUrl"></param>
/// <returns></returns>
public static string ShowModalDialogJavascript(string webFormUrl)

...{
string js=@"<script language=javascript>
var iWidth = 0 ;
var iHeight = 0 ;
iWidth=window.screen.availWidth-10;
iHeight=window.screen.availHeight-50;
var szFeatures = 'dialogWidth:'+iWidth+';dialogHeight:'+iHeight+';dialogLeft:0px;dialogTop:0px;center:yes;help=no;resizable:on;status:on;scroll=yes';
showModalDialog('"+webFormUrl+"','',szFeatures);</script>";
return js;
}

public static string ShowModalDialogJavascript(string webFormUrl,string features)

...{
string js=@"<script language=javascript>
showModalDialog('"+webFormUrl+"','','"+features+"');</script>";
return js;
}


/**//// <summary>
/// 函数名:ShowModalDialogWindow
/// 功能描述:打开模式窗口
/// </summary>
/// <param name="webFormUrl"></param>
/// <returns></returns>
public static void ShowModalDialogWindow(string webFormUrl)

...{
string js=ShowModalDialogJavascript(webFormUrl);
HttpContext.Current.Response.Write(js);
}

public static void ShowModalDialogWindow(string webFormUrl,string features)

...{
string js=ShowModalDialogJavascript(webFormUrl,features);
HttpContext.Current.Response.Write(js);
}
public static void ShowModalDialogWindow(string webFormUrl,int width,int height,int top,int left)

...{
string features = "dialogWidth:"+width.ToString() + "px"
+";dialogHeight:" + height.ToString() + "px"
+";dialogLeft:" + left.ToString() + "px"
+";dialogTop:" + top.ToString() + "px"
+";center:yes;help=no;resizable:no;status:no;scroll=no";
ShowModalDialogWindow(webFormUrl,features);
}

public static void SetHtmlElementValue(string formName,string elementName,string elementValue)

...{
string js=@"<Script language='JavaScript'>if(document."+formName+"." + elementName +"!=null){document."+formName+"." + elementName +".value ="+ elementValue +";}</Script>";
HttpContext.Current.Response.Write(js);
}
}
}
3、在button的onClick事件中输入
this.RegisterClientScriptBlock("e","<scriptlanguage=javascript>history.go(-2);</script>");
三、弹出框
1、 弹出提示框
Alert("提示框,呵呵");
2、 弹出确认框
return confirm("确认框,呵呵");
四、其它常用JS
1. oncontextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键
<table border oncontextmenu=return(false)><td>no</table> 可用于Table
2. <body onselectstart="return false"> 取消选取、防止复制
3. onpaste="return false" 不准粘贴
4. oncopy="return false;" oncut="return false;" 防止复制
5. <link rel="Shortcut Icon" href="favicon.ico"> IE地址栏前换成自己的图标
6. <link rel="Bookmark" href="favicon.ico"> 可以在收藏夹中显示出你的图标
7. <input style="ime-mode:disabled"> 关闭输入法
8. 永远都会带着框架 <script language="JavaScript"><!-- if (window == top)top.location.href = "frames.htm"; //frames.htm为框架网页 // --></script>
9. 防止被人frame <SCRIPT LANGUAGE=JAVASCRIPT><!-- if (top.location != self.location)top.location=self.location; // --></SCRIPT>
10. <noscript><iframe src=*.html></iframe></noscript> 网页将不能被另存为
11. <input type=button value=查看网页源代码 onclick="window.location = /'view-source:/'+ /'http://www.youkuaiyun.com//'">
12. 怎样通过asp的手段来检查来访者是否用了代理
<% if Request.ServerVariables("HTTP_X_FORWARDED_FOR")<>"" then
response.write "<font color=#FF0000>您通过了代理服务器,"& _ "真实的IP为"&Request.ServerVariables("HTTP_X_FORWARDED_FOR") end if %>
13. 取得控件的绝对位置
//Javascript
<script language="Javascript">
function getIE(e)
{ var t=e.offsetTop;
var l=e.offsetLeft;
while(e=e.offsetParent)
{ t+=e.offsetTop;
l+=e.offsetLeft;
}
alert("top="+t+"/nleft="+l);
}
</script>
//VBScript
<script language="VBScript">
<!-- function getIE()
dim t,l,a,b
set a=document.all.img1
t=document.all.img1.offsetTop
l=document.all.img1.offsetLeft
while a.tagName<>"BODY"
set
a = a.offsetParent
t=t+a.offsetTop
l=l+a.offsetLeft
wend
msgbox "top="&t&chr(13)&"left="&l,64,"得到控件的位置"
end function -->
</script>
14. 光标是停在文本框文字的最后
<script language="javascript">
function cc()
{ var e = event.srcElement;
var r =e.createTextRange();
r.moveStart(/'character/',e.value.length);
r.collapse(true);
r.select(); }
</script> <input type=text name=text1 value="123" onfocus="cc()">
15. 判断上一页的来源
asp: request.servervariables("HTTP_REFERER")
java script: document.referrer
16. 最小化、最大化、关闭窗口
<object id=hh1
classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Minimize">
</object>
<object
id=hh2 classid="clsid:ADB880A6-D8FF-11CF-9377-00AA003B7A11">
<param name="Command" value="Maximize"></object>
<OBJECT id=hh3
classid="clsid:adb880a6-d8ff-11cf-9377-00aa003b7a11">
<PARAM NAME="Command" VALUE="Close"></OBJECT>
<input type=button value=最小化 onclick=hh1.Click()>
<input type=button value=最大化 onclick=hh2.Click()>
<input type=button value=关闭 onclick=hh3.Click()> 本例适用于IE