我怎样才能之前页面加载执行JavaScript的功能? [英] How can I execute Javascript function before Page load?
问题描述
我使用隐藏域Isjsenabled检测客户端的JavaScript是否被启用或禁用。如果启动Javascript,Javascript函数将被解雇,这将隐藏字段的值设置为1。
我要检查从服务器端的Page_Load中隐藏的价值。但问题的Javascript后页面加载被炒鱿鱼。
你有什么建议吗?
HTML的一部分
<脚本类型=文/ JavaScript的>
$(函数(){
$(#&其中;%= Isjsenabled.ClientID%>中)。VAL(1);
});
< / SCRIPT>
<输入ID =Isjsenabled=服务器值=0类型=隐藏/>
code部分,后面
保护无效的Page_Load(对象发件人,EventArgs的){
HtmlInputHidden HDN = this.FindControl(Isjsenabled)作为HtmlInputHidden;
如果(HDN!= NULL)
如果(hdn.Value =空&安培;!&安培; hdn.Value ==0)
Helper.SetCookie(IJE,0);
其他
Helper.SetCookie(IJE,1);
}
想都没想太辛苦你正在尝试的方向走去,那里是支持所谓NOSCRIPT所有浏览器都有点用的标签。
< NOSCRIPT>
< IMG SRC =HTTP://myserver/notify_no_script.aspx的风格=显示:无>
< / NOSCRIPT>
正如你所看到的,这个想法是让浏览器请求的图像,但只有当JavaScript已禁用。此页面可以设置一个会话变量,指出当前客户端,这将是提供给所有的code在当前会话中没有脚本的能力。
或者,你可以按照你目前的方法:
< NOSCRIPT>
<输入类型=隐藏名称=无脚本值=1>
< / NOSCRIPT>
<脚本><! -
document.writeline('<输入类型=隐藏名称=无脚本值=0>');
// - >< / SCRIPT>
希望这有助于
-Oisin
I am using a hidden field 'Isjsenabled' to detect whether Client's Javascript is enabled or disabled. If Javascript is enabled, Javascript function will be fired and it will set hidden field's value to 1.
I want to check the hidden value from server side Page_load. But the problem is Javascript gets fired after Page load.
Do you have any suggestion ?
Html Part
<script type="text/javascript">
$(function() {
$("#<%= Isjsenabled.ClientID %>").val(1);
});
</script>
<input id="Isjsenabled" runat="server" value ="0" type="hidden" />
Code Behind Part
protected void Page_Load(object sender, EventArgs e) {
HtmlInputHidden hdn = this.FindControl("Isjsenabled") as HtmlInputHidden;
if (hdn != null)
if (hdn.Value != null && hdn.Value == "0")
Helper.SetCookie("IJE", "0");
else
Helper.SetCookie("IJE", "1");
}
Without thinking too hard about the direction you're trying to go in, there is a little used tag that is supported by all browsers called NOSCRIPT.
<noscript>
<img src="http://myserver/notify_no_script.aspx" style="display:none">
</noscript>
As you can see, the idea is to make the browser request an image but only if javascript is disabled. This page could set a session variable stating that the current client has no script capability that would be available to all code in the current session.
Or, you could follow your current methodology:
<noscript>
<input type="hidden" name="noscript" value="1">
</noscript>
<script><!--
document.writeline('<input type="hidden" name="noscript" value="0">');
//--></script>
Hope this helps,
-Oisin
这篇关于我怎样才能之前页面加载执行JavaScript的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!