Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
554 views
in Technique[技术] by (71.8m points)

.net - How can I execute Javascript function before Page load?

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");
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...