jquery - Adding scripts in MVC -
i starting first mvc application. here have basic confusion. default _layout.cshtml file created below
<!doctype html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@viewbag.title</title> @styles.render("~/content/css") @scripts.render("~/bundles/modernizr") </head> <body> <script> </script> @renderbody() @scripts.render("~/bundles/jquery") @rendersection("scripts", required: false) </body> </html>
i used basic template not have template. check '@scripts.render("~/bundles/jquery")' line. after @renderbody(). adds after body section.
i think best practice. if add $.(document).ready shows following error
microsoft jscript runtime error: '$' undefined
based on error, because of script tag. moved '@scripts.render("~/bundles/jquery")' line before @render body , final page like
<!doctype html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width" /> <title>@viewbag.title</title> @styles.render("~/content/css") @scripts.render("~/bundles/modernizr") @scripts.render("~/bundles/jquery") </head> <body> <script> </script> @renderbody() @rendersection("scripts", required: false) </body> </html>
the applicationw works fine jquery.
so why happened? script adding tag need before @renderbody ? why default template showing in wrong location?
this happens because adding scripts before jquery reference in page. need write scripts inside scripts
section in views:
@section scripts { //stuff }
this make sure rendered after jquery reference(rendersection
call).
Comments
Post a Comment