dongliushui2001 2018-01-28 12:08
浏览 123
已采纳

如何在整页加载(javascript)后加载OpenCart 3中每个页面的内容?

I would appreciate it if someone can point me into the right direction on this issue.

I'm trying to optimize page load speed of my OpenCart project. To do this I want to load the content of each page (product/category , product/search, ...) after javascript onload like this in header.twig:

window.onload = function() {
    console.log('page loaded');

    $.post( window.location.href, { loaded: true })
        .done(function( data ) {
            alert( "Data Loaded: ");
    });
}

In php side I tried such a code but it's not successful at all.

if (isset($this->request->get['manufacturer_id']) && isset($_POST['loaded'])) {
...

If I want to use callback the problem is pages are different. I don't know how to manage all pages.

  • 写回答

1条回答 默认 最新

  • douchang6770 2018-01-29 12:09
    关注

    SOLUTION

    To fix the issue I removed all window.onload = function()s from header, footer, etc. Instead, I added it to common/home, product/search, etc, and it works fine.

    window.onload = function() {
    
    var productClass = 'product-layout product-full col-lg-3 col-md-4 col-sm-6 col-xs-12';
    
    $('#moreHolder').css("display", "block");
    
    $.post('index.php?route=product/productx', { class: productClass },
    function(data) {
         if(data.length>10){
            $(data).insertBefore($('#moreHolder'));
         }
    })
    .always(function() {
        $('#moreHolder').css("display", "none");
    });
    

    I also added swiper related style and javascript on each page's first load, checking it by the number of products/limit = page to not have duplicates:

    {% if products %} 
    {% if page <= 1 %} 
    <link href="catalog/view/javascript/jquery/swiper/css/swiper.min.css" rel="stylesheet" type="text/css">
    <link href="catalog/view/javascript/jquery/swiper/css/opencart.css" rel="stylesheet" type="text/css">
    <script src="catalog/view/javascript/jquery/swiper/js/swiper.jquery.js" type="text/javascript"></script>
    {% endif %} 
    {% for product in products %}
    ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python随机森林对两个excel表格读取,shap报错
  • ¥15 基于STM32心率血氧监测(OLED显示)相关代码运行成功后烧录成功OLED显示屏不显示的原因是什么
  • ¥100 X轴为分离变量(因子变量),如何控制X轴每个分类变量的长度。
  • ¥30 求给定范围的全体素数p的(p-2)/p的连乘积值
  • ¥15 VFP如何使用阿里TTS实现文字转语音?
  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip