3

How to add link to JavaScript file into the header of the certain ASP .NET MVC page?

Lets say there are _Layout.cshtml and About.cshtml and I need to put some javascript file to the header of the About.cshtml. I mean to that page only.

http://www.dotnetcurry.com/ShowArticle.aspx?ID=636

How it can be done?

2
  • 2
    check this out: stackoverflow.com/questions/5110028/… Commented Nov 9, 2012 at 23:44
  • 2
    I personally think that the second answer on that link is better than the accepted one. Commented Nov 9, 2012 at 23:45

3 Answers 3

5

My solution:

_Layout.cshtml

!DOCTYPE html>
<html>
<head>
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>


    @if (IsSectionDefined("AddToHead"))
    {
         @RenderSection("AddToHead", required: false)
    }    
</head>

About.cshtml

@{
    ViewBag.Title = "Index";     
}

<h2>Index</h2>

@section footer {
     <b>Footer Here</b>
}

@section AddToHead {
    <script src="@Url.Content("~/Scripts/test.js")" type="text/javascript">
    </script>
} 
Sign up to request clarification or add additional context in comments.

3 Comments

I think you don't need to verify if section defined when you don't have else part (default content)
You should at least give credit to the author of this approach that you adapted: stackoverflow.com/a/7357102/1026459
@user636525 I am sure It will.
4

Why must it be in the header? You can include any script you need inline:

<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

5 Comments

Sometimes it doesn't work if the script is not in the header... Some of JQuery plugins are not working I mean.
@Peretz - Never that I have come across, can you provide a working example of that happening?
@TravisJ Situation I just came across at work. cshtml files were including js files. When the section was reloaded via json, there was a bunch of global variables (working on getting rid of those) that were getting mauled because of the reload. So there's one example.
@Tom - Usually people prevent that by checking for existence before initialization. For example var myInitializer = myInitializer || init();. I think my previous comment was probably poorly worded, in that order can definitely affect the script, however it will still load.
Which is what I did to fix our issue (added the OR), but it's still a case where this applies. Working on an application with a lot of those types of variables. Having said that, I now realize that this post is 2 years old. ;)
1

Try below code

HtmlGenericControl my_js = new HtmlGenericControl("script");
            my_js.Attributes["async"] = "async";
            my_js.Attributes["type"] = "text/javascript";
            my_js.Attributes["src"] = "http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js";
            Page.Header.Controls.Add(my_js);

You need to use namespace : "using System.Web.UI.HtmlControls" to use HtmlGenericControl.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.