16

How do I inject a script tag such as

<script src="somejsfile"></script>

or

<script type="text/javascript>some javascript</script>

into the head tag of a page from a partial view?


Update: Answer for the old question This is about ASP.NET MVC. We can use the RenderSection. Here the sample for MVC 3 using Razor view engine:

layout view or master page:

<html>
  <head>
  <script ...></script>
  <link .../>
  @RenderSection("head")
  </head>
  <body>
  ...
  @RenderBody()
  ...
  </body>
</html>

View, e.g. Home:

@section head{
  <!-- Here is what you can inject the header -->
  <script ...></script>
  @MyClass.GenerateMoreScript()
}
<!-- Here is your home html where the @RenderBody() located in the layout. -->
CallMeLaNN
  • 8,328
  • 7
  • 59
  • 74
vakman
  • 494
  • 4
  • 12

2 Answers2

2

Even if THX's answer works, it's not a good one as MVC's intention is to be stateless by nature. Moreover his solution does not let you place your scripts where they should go - most sites want their script declarations at the very bottom of their page just before the </body> tag as it is best for performance and search engine optimization.

There is an answer, thanks to Darin Dimitrov:

Using sections in Editor/Display templates

His answer allows you to register scripts from a partial view within the layout view.

The caveat is his answer marks items using a GUID in a dictionary object, so there is no guarantee the scripts will be rendered in the master page in the same order they are listed in your partial view.

There are two workarounds for that:

  1. Register only 1 script from a partial view -or-
  2. Change his HTML Helper implementation to support ordering

I'm trying to work on the later myself.

Good luck.

Community
  • 1
  • 1
one.beat.consumer
  • 9,414
  • 11
  • 55
  • 98
0

Partial views are UserControls. Can't you use RegisterClientScriptInclude method of ClientScriptManager?

protected override void OnLoad(EventArgs e) {
    base.OnLoad(e);
    Page.ClientScript.RegisterClientScriptInclude("some key", "http://website/javascript.js");
}
THX-1138
  • 21,316
  • 26
  • 96
  • 160
  • 2
    This applies to ASP.NET WebForms, but not MVC. – Chris Pietschmann Nov 20 '09 at 15:22
  • 1
    This will work in MVC when working off a view but it won't work in a partial view as there's now way to inherit a master page. – vakman Nov 21 '09 at 07:39
  • Your edit is definitely a web form approach and won't work in MVC. – vakman Nov 22 '09 at 19:45
  • MVC is built on top of ASP.NET, so `Page_Load` event and `Page.ClientScript` behave the same as in "Web Forms" app. I just tried it. Though `RegisterClientScriptInclude` puts the – THX-1138 Nov 22 '09 at 20:15
  • Interesting. If it doesn't put it in the head though then I may as well just put the script tag directly in the view. – vakman Nov 23 '09 at 07:19
  • 1
    HTML 4.0 18.2.1 : "The SCRIPT element places a script within a document. This element may appear any number of times in the HEAD or BODY of an HTML document." So yes, script in body is OK. – THX-1138 Nov 23 '09 at 16:08
  • Yes but you don't necessarily want the script repeated for each instance of the control. Better to include a code block or reference an external file by inserting it into a single place. The OPs update is the correct way to do it. It's not always about just sticking to spec. Use ViewData to ensure you only get one copy though so you don't get an inclusion for every instance. – enashnash Mar 22 '11 at 15:09
  • Bad idea in MVC as it should be stateless. See my (Darin Dimitrov's) answer below. – one.beat.consumer Dec 06 '11 at 23:49