Adding js reference AND actual js into _Root.Head.cshtml without using a controller action

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
6 years ago
I need to inject an external js reference as well as js script for a mailchimp popup form. This is the code they provided:

<!--mailchimp popup form-->
    <script type="text/javascript" src="//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script>
    <script type="text/javascript">require(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us2.list-manage.com","uuid":"me","lid":"you"}) })</script>


I know I could do the external script reference like so:
Html.AddScriptParts(ResourceLocation.Foot, "//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js", true);


But this doesn't account for the other parameters:
data-dojo-config="usePlainJson: true, isDebug: false"
. Not sure if you can do that with AddScriptParts?

Next I could add the actual js into another local js file and include it the same way.

But how can you do both in one file. Essentially like the old @include for classic ASP where I can put these two lines (the external reference and actual js) into one file and have it injected into the header? Why? Namely for ease of re-integrating when upgrading or changing themes.

I see you could use a controller action to return content and call that in the header like the blog:
@Html.Action("RssHeaderLink", "Blog")
.

But is there a way to do this with client side code and not having to create a whole new controller and compile, etc?
6 years ago
This is somewhat ambiguous. You don't need a controller to add a js reference to a view. Just add it like you would  any other script tag. Maybe the title of your question isn't actually indicative of what you are trying to achieve?
6 years ago
The title is accurate. I want to "Add a js reference AND actual javascript into _Root.Head.cshtml"
I know you don't need a controller to add a js reference, but that is not what I'm looking for. I want to add an external js reference AND actual javascript (this makes calls to that external js referenced file).

Ideally I don't want to add separate script references for each third party toolset we use in the _Root.Head.cshtml file. Instead I'd like one reference to a single file that has all my third party external js file references as well as any javascript function calls in that same file.

So for example if you could have a script reference in _Root.Head.cshtml
<script src="/Scripts/my_thir-party_js.js"></script>


Then in my_thir-party_js.js:
    
<script type="text/javascript" src="//s3.amazonaws.com/downloads.mailchimp.com/js/signup-forms/popup/embed.js" data-dojo-config="usePlainJson: true, isDebug: false"></script>

<script type="text/javascript">
        require(["mojo/signup-forms/Loader"], function(L) { L.start({"baseUrl":"mc.us2.list-
        manage.com","uuid":"me","lid":"you"}) })
</script>


But that obviously doesn't work. As such, how else can you achieve this. You basically need a way to insert a regular text file containing this code that would then be injected into the header. Which is what @Html.Action("RssHeaderLink", "News") does, which is a controller action.


seanrock wrote:
This is somewhat ambiguous. You don't need a controller to add a js reference to a view. Just add it like you would  any other script tag. Maybe the title of your question isn't actually indicative of what you are trying to achieve?
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.