Load script file dynamically in ASP.NET MVC

Deadly 提交于 2019-12-08 04:54:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!