I have a method that adds javascript to the end of partial views if it's an ajax request. My problem though is that when the script is returned, the script tags have been removed and the function I want to execute is written in plain text.
if (httpContext.Request.IsAjaxRequest())
{
script = "MyNamespace.globals.initFunctions.push(function() { " + script + " });";
return new HtmlString(@"<script type=""text/javascript"">" + script + "</script>");
}
So, instead of getting the desired result of
<script type="text/javascript">MyNamespace.globals.initFunctions.push(function() { MyNamespace.init(); });</script>
I get MyNamespace.globals.initFunctions.push(function() { MyNamespace.init(); }); in plain text.
What could be the reason?
Edit:
Trying it with Html.Raw() did not help. While the script string does contain the script tags, they are still removed when rendered.
@{
string script = ViewUtilities.AddScript("MyNamespace.init();", this.ViewContext.HttpContext);
}
@Html.Raw(script);
Edit 2:
Now I've tried writing it all in the view like this,
<script type="text/javascript">
MyNamespace.globals.initFunctions.push(function() { MyNamespace.init(); });
</script>
and it still removes the tags around the script and renders it as plain text. I don't know where to go from here...
When I exam the response with Firebug it looks fine but looking at the html it's not.