dongliushui2001 2018-01-28 04: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 04: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 雄安新区高光谱数据集的下载网址打不开
  • ¥66 android运行时native和graphics内存详细信息获取
  • ¥100 求一个c#通过CH341读取数据的Demo,能够读取指定地址值的功能
  • ¥15 rk3566 Android11 USB摄像头 微信
  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部