Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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");
}
like image 617
miti737 Avatar asked May 20 '09 02:05

miti737


People also ask

How do you run a function before the page is loaded in JavaScript?

The first approach for calling a function on the page load is the use an onload event inside the HTML <body> tag. As you know, the HTML body contains the entire content of the web page, and when all HTML body loads on the web browser, it will call the function from the JavaScript.

Can I run JavaScript before the whole page is loaded?

You can put it on a type="module" tag, or use it instead of defer on a classic script tag. That way, even though the code is run as soon as its encountered, all of the elements defined by the HTML above it exist and are ready to be used.

What are the ways to execute JavaScript after page load?

Method 1: Using onload method: The body of a webpage contains the actual content that is to be displayed. The onload event occurs whenever the element has finished loading. This can be used with the body element to execute a script after the webpage has completely loaded.

How do I run a JavaScript function on a website?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.


2 Answers

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

like image 183
x0n Avatar answered Sep 19 '22 19:09

x0n


you should draw your script or noscript header as a page onto itself that then redirects (either through a meta tag or through a script location.href) to the next place you want to go based on if javascript is turned on. It'll take 2 steps, but it'll get you the result you're looking for. You can pass the information you need back to yourself in the query of the redirect.

like image 21
Yevgeny Simkin Avatar answered Sep 17 '22 19:09

Yevgeny Simkin