I am facing the following situation.
I use ajax to communicate with the backend which responds with mixed html/js code. I use the html() function to load my content in a div - i.e. $("#mydiv").html(ajaxResponse);
However, the js code embedded in <script> tags in the ajaxResponse runs in global (window) context and not my predefined one.
Is there any way to change the context of execution in this situation?
code looks like the following
index.html:
<div id="mydiv"></div>
<script type="text/javascript">
$(function(){
$.ajax({
url: '/myAjaxResponse.html',
context: $(this),
success: function(resp) { $("#mydiv").html(resp); }
});
});
</script>
myAjaxResponse.html:
<!-- Some html... really just anything :) -->
<script type="text/javascript">
console.log($(this)); // $(this) points to window object :(
</script>
#mydiv?