Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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 ??

share|improve this question
    
Have you checked what's the value of scripts variable after filter? Are there no errors in console? – Konrad Gadzina Apr 24 '13 at 7:00
    
"scripts" is an array of script objects and if you put "this.src" in foreach statement it gives you complete script tag. There's no error when injection is take place. – Pouyan Apr 24 '13 at 7:12

1 Answer 1

up vote 0 down vote accepted

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);
  }
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.