How can I execute Javascript function before Page load?

前端 未结 4 520
清歌不尽
清歌不尽 2020-12-18 00:48

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 w

相关标签:
4条回答
  • 2020-12-18 00:48

    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

    0 讨论(0)
  • 2020-12-18 01:00

    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.

    0 讨论(0)
  • 2020-12-18 01:02

    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.

    0 讨论(0)
  • 2020-12-18 01:04

    javascript can set cookie by itself.

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

    Does this resolve your problem?

    0 讨论(0)
提交回复
热议问题