How to load page synchronously using jQuery

后端 未结 6 1160
遥遥无期
遥遥无期 2021-01-23 04:03

Currently I am using jQuery to choose what my index page should load depending whether a cookie is set.

For example, the body tag of the index.html should load hasCookie

6条回答
  •  甜味超标
    2021-01-23 04:42

    This is how i do it.

    You can attach Style and Javascript upon callback from the Ajax Function.

      $.ajax({
        url: somePage,
        success:function(ajaxData){
    
                            //ATTACH AND RUN CORRESPONDING PAGE SCRIPT
                            var script = somePage + '.js';
                            $.getScript(script);
    
    
    
                            //ATTACH CORRESPONDING STYLE SHEET
                            var style = document.createElement('link');
                            style.type = 'text/css';
                            style.href = '/css/'+somePage+'.css';
                            style.rel = 'stylesheet';
                            style.media = 'screen';
    
                            document.getElementsByTagName('head')[0].appendChild(style);
    
                           //ADD HTML RETURNED
                           $('body').html(ajaxData);      
    
    
    });
    

    This allows everything to be loaded including styles and javascript, given that the script name and css file name are the same.

提交回复
热议问题