使用ajax从页面加载JS

I use $('.class').load('url #id'), but it get only html code, or I'm doing something wrong)

How get JS from page?

Site where I use this code: http://vbtes.p.ht/Amira/#!contacts (haven't js, don't work tabs).

Have JS: http://vbtes.p.ht/Amira/#!contacts

var sm = 1;
var oldactivemenu = 0;
var selectedmenu = '';
var elemthis;

function ocmenu(heshatr, thiselem) {
    elemthis = thiselem;
    if (sm == 1) {
        location.hash = '!' + heshatr;
    }
}
$(function () {
    $(window).hashchange(function () {
        // Alerts every time the hash changes!
        var linkmenuurl = $(location).attr('href').split("#!")[1];
        if (location.hash == '') {
            linkmenuurl = '';
        }
        if (sm == 1 && selectedmenu !== linkmenuurl) {
            $('.active').removeClass('active');
            $(elemthis).addClass('active');
            if ($(elemthis).parent('.main-menu__item_level_1')) {
                $(elemthis).parent().parent().parent().children('.main-menu__item-link').addClass('active');
            }

            selectedmenu = linkmenuurl;
            sm = 0;
            $('.new').load(linkmenuurl + ' #lay_body_get', function () {
                $('.new').show();
                $('.lb2').css({
                    top: $('.lb1').height() * -1 - 60
                });
                $('.new').animate({
                    left: 0
                }, 500);
                $('.old').animate({
                    left: -1450
                }, 500, function () {
                    $('.old').addClass('ob1');
                    $('.new').addClass('nw1');
                    $('.ob1').removeClass('old').addClass('new').css({
                        left: 1450
                    }).hide(100);
                    $('.nw1').removeClass('new').addClass('old').animate({
                        top: 0
                    }, 100, function () {
                        sm = 1;
                    });
                    $('.ob1').removeClass('ob1');
                    $('.nw1').removeClass('nw1');

                });
            });
        }
    })

    // Trigger the event (useful on page load).
    $(window).hashchange();
});

When you call .load using a selector, jQuery will strip the script from the result.

From the jQuery documentation:

Script Execution

When calling .load() using a URL without a suffixed selector expression, the content is passed to .html() prior to scripts being removed. This executes the script blocks before they are discarded. If .load() is called with a selector expression appended to the URL, however, the scripts are stripped out prior to the DOM being updated, and thus are not executed. An example of both cases can be seen below:

Use $.getScript(url) as documented here.

If those are <script> tags inside the loaded HTML, iterate over them with jQuery, and load them using the function above.

Addition: You could do something like:

$.ajax(myPageURL, { complete: function (data) { 
    $(data.responseText).filter('script').each(function () { 
        $.getScript(this.src);

        // OR

        $('head').append($(this).html());
    }); 
}});

This is a workaround to the jQuery.load() issue. It loads the entire page again, but strips the <html>, <body>, <head> (and etc.) element - thus allowing you to filter the scripts in the page's header. Note that this will cause the page to load a second time.

BTW, adding a <script> tag to your page instead of using $.getScript() enables debugging of the script in Chrome, at the very least.