How to to include JavaScript into the page header MVC4 [duplicate]

Possible Duplicate:
ASP.Net MVC 3 Razor: Include js file in Head tag

I don't want to put a lots of JS into some layout and I need to do it for some specific pages I mean to include some of the JS into their header.

I've tried like that but it doesn't work as it should be.

@{
    Layout = "~/Views/Shared/_LayoutInner.cshtml";
    @Scripts.Render("~/Scripts/farbtastic/farbtastic.js")
    @Styles.Render("~/Scripts/farbtastic/farbtastic.css")
    @Scripts.Render("~/Scripts/jquery.tinycarousel.min.js")
    @Scripts.Render("~/Scripts/jquery-ui-1.8.11.min.js")
}
<script type="text/javascript" charset="utf-8">
    $(document).ready(function () {
        $('#slider1').tinycarousel();
        $("#accordion").accordion();
        $('#picker').farbtastic('#color');
    });
</script>

I have tried like that

@{
    Layout = "~/Views/Shared/_LayoutInner.cshtml";
  <script type="text/javascript" src="@Url.Content("~/Scripts/farbtastic/farbtastic.js")"></script>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery.tinycarousel.min.js")"></script>
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
}

and no success at all.

How I can archive it?

Answers:

Answer

I'm sure in your _LayoutInner.cshtml you should have refered JS files similarly like this

<head>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
</head>

To achieve your target you have to add two named sections into your _LayoutInner.cshtml pages head section like this-

<head>
<link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")"></script>
 @RenderSection("JavaScript", required: false)
 @RenderSection("CSS", required: false)
</head>

Now in your other pages to include extra javascript or css pages use these named sections

@section JavaScript
{
   <script type="text/javascript"src="@Url.Content("~/Scripts/farbtastic/farbtastic.js")"></script>
   <script type="text/javascript" src="@Url.Content("~/Scripts/jquery.tinycarousel.min.js")"></script>
}

@section CSS
{
  <link href="@Url.Content("~/Scripts/farbtastic/farbtastic.css")" rel="stylesheet" type="text/css" />
}

It is upto you whether to include different named sections for javascript and css.

Hope it helps!

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.