What is the best way to add javascript at runtime from a view (or partialview) ?
For example i would like to build a partial view Banner.cshtml that use Banner.Js .
Actually i am using a section into layout page but obviusly if i use 3 times the same partial view i have 3 reference to external Js.
Is there a "best practice" to include Js from "child" elemnts like partial view in this case?
I don't know about a "best practice"; if you can add the needed scripts to the parent view ahead of time, that would work, but then its not as "plug and play". I've in the past created a helper extension that adds the scripts internally, and renders them
at the bottom of the page. This type of implementation would require adding scripts to the bottom of the page, as that is the nature of MVC. Otherwise, you'd have to manually register the scripts in advance if you want control over the placement.
HTH.
mvc-3
Brian
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
If i understand you suggest to write manually script reference to view adding those script that i know have to be used depending on used partialview.
Otherwise you suggest an helper that write the reference for me...did you have any additional suggestion about this helper? It was an helper that directly wrote to the response stream? I i understand this second solution could be a "plug and play" because
i can use helper directly from partial view without worry about link to add manually.is it right?
there is no good built-in support for this. an easy approach is too create two partial views, the main one and a dependency view that you include in the header. the dependency view can include css and javascript.
you could also pass a parameter (or keep state in the model) on whether to output the javascript.
4asp
Member
15 Points
7 Posts
MVC3 Razor add script from partial view
Feb 17, 2011 04:04 PM|LINK
What is the best way to add javascript at runtime from a view (or partialview) ?
For example i would like to build a partial view Banner.cshtml that use Banner.Js .
Actually i am using a section into layout page but obviusly if i use 3 times the same partial view i have 3 reference to external Js.
Is there a "best practice" to include Js from "child" elemnts like partial view in this case?
thankyou
mvc-3
bmains
All-Star
29116 Points
5886 Posts
MVP
Re: MVC3 Razor add script from partial view
Feb 17, 2011 04:10 PM|LINK
Hello,
I don't know about a "best practice"; if you can add the needed scripts to the parent view ahead of time, that would work, but then its not as "plug and play". I've in the past created a helper extension that adds the scripts internally, and renders them at the bottom of the page. This type of implementation would require adding scripts to the bottom of the page, as that is the nature of MVC. Otherwise, you'd have to manually register the scripts in advance if you want control over the placement.
HTH.
mvc-3
"Trust in the Lord and do what is good; dwell in the land and live securely. Take delight in the Lord, and He will give you your heart's desires" (Psalm 37: 3-4).
4asp
Member
15 Points
7 Posts
Re: MVC3 Razor add script from partial view
Feb 17, 2011 04:47 PM|LINK
If i understand you suggest to write manually script reference to view adding those script that i know have to be used depending on used partialview.
Otherwise you suggest an helper that write the reference for me...did you have any additional suggestion about this helper? It was an helper that directly wrote to the response stream? I i understand this second solution could be a "plug and play" because i can use helper directly from partial view without worry about link to add manually.is it right?
bruce (sqlwo...
All-Star
36852 Points
5446 Posts
Re: MVC3 Razor add script from partial view
Feb 17, 2011 07:48 PM|LINK
there is no good built-in support for this. an easy approach is too create two partial views, the main one and a dependency view that you include in the header. the dependency view can include css and javascript.
you could also pass a parameter (or keep state in the model) on whether to output the javascript.
JLWicker
Member
2 Points
1 Post
Re: MVC3 Razor add script from partial view
Jun 22, 2011 03:11 AM|LINK
RAZOR mvc-3
Madushan Nan...
Member
2 Points
1 Post
Re: MVC3 Razor add script from partial view
Jul 14, 2011 03:05 PM|LINK
loadScriptRef('scripts/script.js');function loadScriptRef(filename) {var fileref = document.createElement('script')
fileref.setAttribute("type", "text/javascript")
fileref.setAttribute("src", filename);
if (typeof fileref != "undefined")
document.getElementsByTagName("head")[0].appendChild(fileref);
}
Weitzhandler
Member
191 Points
145 Posts
Re: MVC3 Razor add script from partial view
Nov 13, 2012 05:46 AM|LINK
Really you should make NuGet package for it!