locked
Script Tag RRS feed

  • Question

  • User-797751191 posted

    Hi

      Where Script tag should be written . What is the advantage of writing before form. What happens if it is written in Head tag

    <head id="Head1" runat="server">
        <title></title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'/>
     
        <link href="Css/custom.css" rel="stylesheet" type="text/css"/>
        <!-- bootstrap 3.0.2 -->
        <link href="Css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <form>
    <div>
    <!-- ./wrapper -->
            <!-- jQuery 2.0.2 -->
            <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
            <!-- Bootstrap -->
            <script src="js/bootstrap.min.js" type="text/javascript"></script>
     
            <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
            <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
            <script>
                $(function () {
                    $(".datepicker").datepicker(
                         {
                             dateFormat: 'dd-mm-yy',
                             maxDate: new Date
                         });
                });
            </script>
        </form>
    </div>
    </body>
    Thanks
    Wednesday, June 26, 2019 5:42 PM

All replies

  • User475983607 posted

    What is the advantage of writing before form.

    None really,  Modern patterns add script tags to the bottom of the page.

    What happens if it is written in Head tag

    You'll need to wrap any element handlers in a ready event or function otherwise the the HTML element will not exist when the script loads.

    Wednesday, June 26, 2019 5:47 PM
  • User753101303 posted

    Hi,

    Historically, the idea was likely to define all what the page depends on at the top. But as scripting became widespread and more complex it caused a delay (loading, parsing, running js code) in rendering the page so the pattern is now rather to place scripts at the end so that the HTML/CSS can processed and rendered first.

    BTW we have now a defer attribute on script tags which should have the same effect that moving the script down (but having scripts at the end is now an habbit).

    Friday, June 28, 2019 9:33 AM