admin管理员组文章数量:1277285
I need a javascript code which loads an image into browser cache. What is the usage? read this:
When the user logs into my site, she/he gets redirected to a page which is "Redirecting you to control panel" and a progress is displayed there too. Now, this "redirector" page has a background, since user experience this page and sees it only 3 seconds, many times, background image is missed and there remains no chance for it to be loaded, since from the page load till the page redirection there is only 3 seconds gap. Here is en example of my ajax login:
$.ajax({
// do ajax stuff
success : function(msg)
{
if(msg==true)
{
// I NEED A FUNCTION HERE TO LOAD THEM IMAGE INTO CACHE BEFORE THIS PAGE
// TO LOAD THE REDIRECTOR PAGE. USING THIS, I CAN ENSURE THE EXISTENCE OF THE
// BG IMAGE WHEN THE USER SEES NEXT PAGE. THIS BG IMAGE IS INDEED NEXT PAGE'S BG
window.locatio.href = 'process/redirection/to/user-panel';
}
}
});
I need a javascript code which loads an image into browser cache. What is the usage? read this:
When the user logs into my site, she/he gets redirected to a page which is "Redirecting you to control panel" and a progress is displayed there too. Now, this "redirector" page has a background, since user experience this page and sees it only 3 seconds, many times, background image is missed and there remains no chance for it to be loaded, since from the page load till the page redirection there is only 3 seconds gap. Here is en example of my ajax login:
$.ajax({
// do ajax stuff
success : function(msg)
{
if(msg==true)
{
// I NEED A FUNCTION HERE TO LOAD THEM IMAGE INTO CACHE BEFORE THIS PAGE
// TO LOAD THE REDIRECTOR PAGE. USING THIS, I CAN ENSURE THE EXISTENCE OF THE
// BG IMAGE WHEN THE USER SEES NEXT PAGE. THIS BG IMAGE IS INDEED NEXT PAGE'S BG
window.locatio.href = 'process/redirection/to/user-panel';
}
}
});
Share
Improve this question
asked Jan 1, 2014 at 4:09
user2739275user2739275
4
- If your image can't load and be displayed in 3 seconds, your image is too large. Possibly use tiling to reduce it's size, perhaps use a CSS gradient instead, or rethink if it really needs to be there. If so, google 'javascript image preload'. – veddermatic Commented Jan 1, 2014 at 4:20
- 1 I would agree with veddermatic. If an ajax doesn't return in 3 seconds for a single image, either your server is terribly under-resourced, or your image is just too large. That being said, this seems to be an application structure problem more than anything. You want to redirect the user then immediately halt at a progress bar? – Clev3r Commented Jan 1, 2014 at 4:22
- 1 Yes it is a very mon structure. The user gets redirected to a page which is redirecting it to another page. Is this not familiar to you? – user2739275 Commented Jan 1, 2014 at 4:31
- 1 thanks for your suggestions. I have checked that now an to some extent you are right, my picture is 167 KB, but preloading could not be avoid as a solution – user2739275 Commented Jan 1, 2014 at 4:38
1 Answer
Reset to default 9This function will work:
function preloadImage(url)
{
var img = new Image();
img.src = "/test/example.jpg";
}
Also, here is a question that discusses something similar, pre-loading images on a splash screen, but the implementation is far more plex.
On the subject, if you don't have to use JavaScript, another solution using CSS and XHTML that could probably work on the redirect page can be found here. Otherwise, the code at the top should work. Hope this helps, good luck.
本文标签: javascriptJS function to preload images before page loadStack Overflow
版权声明:本文标题:javascript - JS function to preload images before page load - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741269007a2368947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论