Skip to content Skip to sidebar Skip to footer

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 hi

Solution 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><imgsrc="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><inputtype="hidden"name="noscript"value="1"></noscript><script><!--
document.writeline('<input type="hidden" name="noscript" value="0">');
//--></script>

Hope this helps,

-Oisin

Solution 2:

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.

Solution 3:

javascript can set cookie by itself.

var myDate = newDate()
document.cookie = 'IJE=1; expires=' + myDate.toGMTString()

Does this resolve your problem?

Solution 4:

Unobtrusive JavaScript may be of interest. Allowing your pages to degrade gracefully will simplify development. Design for no JavaScript initially and enhance the experience. This may not be an option if your application is extremely JavaScript intensive and absolutely requires it to function. In that case using the noscript tag to inform your users that they need JavaScript enabled would be a good idea.

Also, look at x0n's comments. Here a reference to help understand the HTTP request life cycle.

Post a Comment for "How Can I Execute Javascript Function Before Page Load?"