admin管理员组文章数量:1279149
I've made a simple google search code like this:
<div style="text-align:center">
<form method="get" action="">
<img src=".png"><br><input style="width:300px"name="q">
<input type="submit" value="Google Search" >
</form></div>
and in this , code(including the form) and image(google image) loads at the same time.
How can I make the form(input) to load firstly, and than the image?
I've made a simple google search code like this:
<div style="text-align:center">
<form method="get" action="http://www.google./search">
<img src="http://www.google./images/srpr/logo3w.png"><br><input style="width:300px"name="q">
<input type="submit" value="Google Search" >
</form></div>
and in this , code(including the form) and image(google image) loads at the same time.
How can I make the form(input) to load firstly, and than the image?
Share Improve this question edited Dec 8, 2014 at 23:27 Idrizi.A asked Apr 24, 2012 at 17:43 Idrizi.AIdrizi.A 12.1k14 gold badges51 silver badges90 bronze badges 1- 3 And why would you want to do that? You can add the img tag when the site is loaded using js. – Michał Miszczyszyn Commented Apr 24, 2012 at 17:48
6 Answers
Reset to default 5You can set the src
of the image once the DOM is ready..
<div style="text-align:center">
<form method="get" action="http://www.google./search">
<img id="googleimage" data-src="http://www.google./images/srpr/logo3w.png"/><br>
<input style="width:300px"name="q">
<input type="submit" value="Google Search" >
</form>
</div>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(){
var img = document.getElementById('googleimage');
img.src = img.getAttribute('data-src');
}, false);
</script>
Demo at http://jsfiddle/gaby/mpEeF/
Just adding that in case that the problem is that as images are loaded (that might be before the form) the form is being pushed down, making hard to locate and edit it, the only solution would be to add actual sizes (width/height through the style attribute) to the images, so that the browser can correctly calculate the final look of the page, even before the images have loaded..
What do you mean by
they are loaded at the same time
because they are not loaded at same time, first the page is loaded and in parallel as img
tag are parsed images are loaded. So actually browser almost does what you want, you can test that by having a img which loads slowly you will see your form will load first.
Anyway what is the actual problem you are trying to solve.
Check this code . You should include jquery in your page or convert jquery code to normal javascript
http://jsfiddle/pt4DT/
I want form(input) to load first (and users to be able to write earlier), because images are not important, I want them to load at the end(after all html, css and js loads)
Ok, first of all let me say that this only is true for all the newer browser up from about IE6 I think (not sure when exactly), but that still should be the vast majority.
Below you can see an image of how a typical page load containing html (the first line being the html, the second js, the third and fourth css and the last the image).
As you can see the html is loaded first as it needs to know which js and css needs to be loaded. Now - and this depends on the browser and blocking/non blocking javascript - the image itself is sometimes loaded in parallel with the js/css or it's loaded once the css and javascript is finished downloading (as per the image above). Either way, whatever the browser decides to do, it will be the most optimal solution (e.g. there is a limit on the number of https request most browser will do in parallel which is solved by the spdy protocol, but that's another story). Now, looking more closely at the graph you can see two lines, one blue line which shows when the page is displayed to the user (with the form, but without the image) and a green line once the DOM is finished loading (including the image). Thus the browser is already doing exactly what you want it to do. However, as there are only about 100ms between the two points you probably don't conciously see this except if you have a lot of images.
The only important thing is to not lock the loading of your page in your javascript which can speed the loading of the page up incredibly (for example by loading most javascript after the page finishes loading by injecting it dynamically into the page). Either way, any optimizations benefit greatly from taking a look at the waterfall of your page load (the image above) in either firefox (with firebug and go to the net tab) or chrome (hit F12 and go to the network tab) showing what is loaded when and trying to get the blue line to move as much as possible to the left.
Try this one :
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/1999/xhtml">
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
<script type="text/javascript">
function Addimg()
{
document.getElementById("googleimg").src = "http://www.google./images/srpr/logo3w.png";
}
</script>
</head>
<body onload="Addimg()">
<div style="text-align:center">
<form method="get" action="http://www.google./search">
<img id="googleimg" src=""><br><input style="width:300px"name="q">
<input type="submit" value="Google Search" >
</form></div>
</body>
</html>
Yet another one:
Try This
HTML:
<div id="google-search-container">
<form method="get" action="//www.google./search">
<div id="google-logo"></div>
<input id="q" name="q" />
<input type="submit" value="Google Search" />
</form>
</div>
JavaScript:
function addLogo() {
document.getElementById('google-logo').style.backgroundImage = "url('//www.google./images/srpr/logo3w.png')";
}
window.onload = function() {
addLogo();
}
CSS:
#google-search-container {
text-align: center
}
#google-logo {
margin: 10px auto;
background-repeat: no-repeat;
background-size:275px 95px;
height: 95px;
width: 275px
}
#q {
width: 300px
}
本文标签: javascriptPreload in htmlStack Overflow
版权声明:本文标题:javascript - Preload in html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741282028a2370061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论