How to load a view with ajax with all js files needed? I am using this function to load three views which are loading comments of a node in three tabs by three different sort types by ajax:

function sort1(){
var nid='&lt;?php echo $node-&gt;nid; ?&gt;';    
$.ajax({
    url: 'http://www.example.com/views/ajax',
    type: 'post',
    data: {
        view_name: 'comments_views',
        view_display_id: 'block_3', 
        view_args: nid,
    },
    dataType: 'json',
    success: function(response){
        $('#cm').html(response[1].data);
    }
});
    return false;   
}


And for other two views there exist a similar function. 

Now what is the problem?

These comments can be replied and voted. But after switching between tabs nether reply nor vote works by ajax. I am using ajax_comment module in order to make it work by ajax and voteAPI and Rate module for rating comments.

I think views is failing in loading js of views.

what is the solution?
What you did is a HardCode solution ,I suggest you instead of loading JS on block, put your JS in .JS files and add ( or attached) to page you want, with simple if your JavaScript just works for you, and remain only one problem, how pass NID to JS?

Pass it with something like     

drupal_add_js(array('YOURMODULE' =&gt; array('nid' =&gt; $nid)), array('type' =&gt; 'setting'));


and get it on your js with 

nid = Drupal.settings.YOURMODULE.nid;


1
down vote

There is a lot of documentation about how to include a JS file into a custom module or into your theme in general. Here is the official article: https://www.drupal.org/node/304255

There are plenty of examples and you can inspire yourself with it.