问题
I'm using ASP.NET MVC. I have a view that loads contents of a partial view in a section.
Problem is that partial view has some script references in it. so when I load contents of that partial view in the main page, script tags get removed to protect view against script injection attacks.
I have tried different ways to filter script tags out of partial page content and load theme separately but I was unsuccessful.
For example I have tried to filter script tags and append them in the head of main page like this :
var scripts = $(content).filter('script');
$(scripts).each(function() {
$('head').append($(this));
I have also tried to load those scripts using $.getscript. The function gets executed nicely but they don't appear in page resource in chrome developer tools. and I cannot use functions inside those scripts.
how can I achieve this ??
回答1:
You can move all references of script to Main view or you can use $.ajax "dataType as script" to load script dynamically:
http://docs.jquery.com/Specifying_the_Data_Type_for_AJAX_Requests
$.ajax({
dataType : 'script',
url : 'http://example.com',
success : function() {
console.log(a);
}
});
来源:https://stackoverflow.com/questions/16185128/load-script-file-dynamically-in-asp-net-mvc