我如何推迟或异步这个词preSS JavaScript片段到最后加载更快的页面加载时间? [英] How do I defer or async this WordPress javascript snippet to load lastly for faster page load times?

查看:219
本文介绍了我如何推迟或异步这个词preSS JavaScript片段到最后加载更快的页面加载时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在我的Word preSS域的一个必要的插件各种JavaScript的,我知道在PHP文件,其中它是从所谓的。

我采取一切措施,我可以采取的加快页面加载时间,然后在网络上每一个速测仪说要推迟脚本如果可能的。

我读过关于在推迟='延迟'异步的功能,我想在javascript中的这些人会完成我试图完成。但我不明白我怎么会在一个PHP文件这样做。

例如,这里是一个特殊的插件的PHP文件的一个片段,其中的JavaScript文件被调用:

 函数add_dcsnt_scripts(){    wp_enqueue_script('jQuery的');
    wp_enqueue_script('dcsnt',dc_jqsocialtabs :: get_plugin_directory()'/js/jquery.social.media.tabs.1.7.1.min.js');}

我读过,这是最好的更快的页面加载时间做这样的事情:

 <脚本延迟异步SRC =...>< / SCRIPT>

但我不知道如何实现这一目标的PHP文件中。我想用我所有的JavaScript文件来做到这一点。

我将如何完成推迟或asyncing此JavaScript代码段是最后的负载,加快页面加载时间?什么是增加在所有浏览器的页面加载时间的理想方式?感谢您的任何指导,任何人都可以提供!


解决方案

博客文章链​​接,感兴趣的两个插件:


  

异步的JavaScript 结果
  的使用head.js异步加载JavaScript提高网页加载速度



  

WP递延的Javascript 结果
  *推迟与wp_enqueue_scripts添加的所有JavaScript的加载,使用LABJS(异步JavaScript库)。*


没有测试过,但检查了code和他们做与Word preSS脚本入队过程pretty看中的东西。

但随后 WPSE来抢救

  //从https://gist.github.com/toscho/1584783改编
的add_filter('clean_url',函数($网址)
{
    如果(FALSE === strpos($网址,'.js文件'))
    {//不是我们的文件
        返回$网址;
    }
    //必须是,而不是!
    返回$ URL'推迟='延迟;
},11,1);

I have various javascripts that are necessary plugins in one of my WordPress domains, and I know where in the php file it's called from.

I'm taking every measure I can take to speed up page loading times, and every speed tester on the web says to defer javascripts if possible.

I have read about the defer='defer' and the async functions in javascript and I think one of these will accomplish what I'm trying to accomplish. But I'm not understanding how I'd do so in a php file.

For instance, here is a snippet from one particular plugin's php file where the javascript file is being called:

function add_dcsnt_scripts() {

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'dcsnt', dc_jqsocialtabs::get_plugin_directory() . '/js/jquery.social.media.tabs.1.7.1.min.js' );

}

I've read that it's best to do something like this for faster page loading times:

<script defer async src="..."></script>

But I don't know how to accomplish that within a php file. I want to do this with all of my javascript files.

How would I accomplish deferring or asyncing this javascript snippet to is loads last and speeds up page load times? What would be the ideal way to increase page load times across all browsers? Thanks for any guidance anybody can offer!

解决方案

This blog post links to two plugins of interest:

Asynchronous Javascript
Improve page load performance by asynchronously loading javascript using head.js

WP Deferred Javascripts
*Defer the loading of all javascripts added with wp_enqueue_scripts, using LABJS (an asynchronous javascript library).*

Haven't tested them but checked the code and they do pretty fancy stuff with WordPress scripts enqueuing process.

But then WPSE comes to rescue:

// Adapted from https://gist.github.com/toscho/1584783
add_filter( 'clean_url', function( $url )
{
    if ( FALSE === strpos( $url, '.js' ) )
    { // not our file
        return $url;
    }
    // Must be a ', not "!
    return "$url' defer='defer";
}, 11, 1 );

这篇关于我如何推迟或异步这个词preSS JavaScript片段到最后加载更快的页面加载时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆