简体   繁体   English

在php中延迟加载Java脚本

[英]lazy loading of java script in php

I am using the Google visualization for drawing graphs into my website. 我正在使用Google可视化工具将图形绘制到我的网站中。 This involves a lod of java script. 这涉及大量的Java脚本。

 <script type="text/javascript" src="http://www.google.com/jsapi"></script>
 <script type="text/javascript">
 google.load('visualization', '1', {packages: ['annotatedtimeline']});
 </script>

These two lines of script takes at least 5 to 8 sec to load initially. 最初加载这两行脚本至少需要5到8秒的时间。 How ever i use the graph feature in my website after sometime of the initial page load and not immediately. 在初始页面加载后的某个时间而不是立即使用我的网站中的图形功能。 So i was wondering instead of slowing down the page load by 5 sex for the script to load can i do a lazy load of java script parallel to my page load. 因此,我想知道不是将页面加载速度降低5个性别,而是让脚本加载时可以并行执行Java脚本的懒加载。 This will make my initial page load faster. 这将使我的初始页面加载速度更快。 Can this be done, if so please let me know syntax and also some link providing the documentation if any. 如果可以的话,可以这样做,请让我知道语法,如果有的话,也请提供文档的链接。

根据此YSlow建议,将所有script标签紧接在结束body标签之前。

Put it as lower as you can at the page, meaning closer to the end tag. 在页面上将其放得越低越好,这意味着离结束标记越近。 Your page will load all its contents before delaying further to load the JS. 您的页面将加载其所有内容,然后再延迟加载JS。

You can also experiment with the DEFER attribute and placing the script tag into a separate head tag: 您还可以尝试使用DEFER属性,并将脚本标签放置在单独的head标签中:

http://www.hunlock.com/blogs/Deferred_Javascript http://www.hunlock.com/blogs/Deferred_Javascript

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM