我正在为我办公室的文件夹开发这个一页的网站,为了保持它的整洁,我使用引导模式来隐藏一些内容(相当多的图像和描述)。根据谷歌PageSpeed Insights的说法,这使得整个页面变得沉重。为了寻找解决方案,我在考虑将所有的模态加载为外部html文件,使用以下代码:
www.jsfiddle.net/qp7NP/我不知道这对我的情况来说是不是最聪明的解决方案。我也在关注搜索引擎优化。我读到过AJAX以某种方式用于动态加载内容,但我对AJAX一点也不熟悉。如果你们中的任何人知道我的情况的任何解决方案或可以在页面速度问题上给我建议,我将非常感激!
谢谢大家!
发布于 2016-03-18 10:13:13
AJAX真的不像听起来那么可怕,对于这样的东西来说,它是一个很好的选择,因为它允许你用几行JavaScript加载大量的内容,这意味着无论额外的内容如何,初始加载时间都更短。
下面的示例演示了如何将“折叠下方”(在起始滚动位置下方)作为HTML加载,并在需要的地方插入。
<!DOCTYPE html>
<html>
<head>
<title>example</title>
</head>
<body>
<h1>Page Title.</h1>
<p>This is the content which is on the screen at load</p>
<!-- this is the div which will hold all content outside of view when the page loads -->
<div id="below_fold"></div>
<script>
// get the content on once the page has loaded
window.onload = function() {
// set up http request object
var xhttp = new XMLHttpRequest();
// tell http object what to do when response is recieved
xhttp.onreadystatechange = function() {
// if its all good then go ahead
if (xhttp.readyState == 4 && xhttp.status == 200) {
// add content to div
document.getElementById("below_fold").innerHTML = xhttp.responseText;
}
// else we don't do anything, you could display an error or try again
};
// set file path for contents
xhttp.open("GET", "below_fold_content_html.txt", true);
// send request, xhttp.onreadystatechange will be called when completed
xhttp.send();
}
</script>
</body>
</html>(上面的示例是对http://www.w3schools.com/ajax/的添加)
https://stackoverflow.com/questions/36052677
复制相似问题