next Posted July 23, 2008 Share Posted July 23, 2008 I just made a preload script, but i thinks it's not working, please let me know what i am doing wrong here: if(document.images) { imgs = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h"); for(var i=0; i<=imgs.size, i++) { imgs[i] = new Image(87, 30); imgs[i].src="assets/" + imgs[i] + ".gif"; alert("assets/" + imgs[i] + ".gif"); } } i used "alert" for testing purposes, that's how i know that it's not working. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/ Share on other sites More sharing options...
adam84 Posted July 23, 2008 Share Posted July 23, 2008 if(document.images) { imgs = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h"); for(var i=0; i<=imgs.size, i++) { imgs[i] = new Image(87, 30); imgs[i].src="assets/" + imgs[i] + ".gif"; alert("assets/" + imgs[i] + ".gif"); } } You have a comma in your loop declaration. Make it for(var i=0; i<=imgs.length; i++) { whatever } See if that works! Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597639 Share on other sites More sharing options...
next Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks for pointing that out, but alert is still not showing up. Do i need some event to trigger this code? Or is it ran automatically on page load? Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597666 Share on other sites More sharing options...
adam84 Posted July 23, 2008 Share Posted July 23, 2008 I dont know think it would run right off the bat, you could add it to your <BODY> tag <BODY ONLOAD=init();> something like that Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597679 Share on other sites More sharing options...
next Posted July 23, 2008 Author Share Posted July 23, 2008 weird, i added a test function: function test() { alert('x'); } and onload event: <body onload="test()" id="doc3" class="yui-t4"> but firebug said: "test is not defined http://localhost/movietalk/index.php Line 1" I can't get even a simple alert to work Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597686 Share on other sites More sharing options...
KevinM1 Posted July 23, 2008 Share Posted July 23, 2008 I dont know think it would run right off the bat, you could add it to your <BODY> tag <BODY ONLOAD=init();> something like that That's an ugly way to do it. Markup should have no knowledge of the script(s) used upon it. Try, instead: <html> <head> <title>Test</title> <script type="text/javascript"> window.onload = function() { var imgs = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h"); for(var i = 0; i < imgs.length; i++) { imgs[i] = new Image(87, 30); imgs[i].src="assets/" + imgs[i] + ".gif"; alert("assets/" + imgs[i] + ".gif"); } } </script> </head> <body> <!-- markup --> </body> </html> Keep in mind, you may be overwriting your initial image values ("home_btn_h", etc) with the new Image objects. To be safe, you should have two arrays: one filled with the image names, one you build as you go on: var imgNames = new Array("home_btn_h", "login_btn_h", "reg_btn_h", "del_forum_btn", "msgs_btn_h", "new_forum_btn_h", "reg_btn_h", "unread_btn_h"); var imgs = new Array(); for(var i = 0; i < imgNames.length; i++) { imgs[i] = new Image(87, 30); imgs[i].src="assets/" + imgNames[i] + ".gif"; alert("assets/" + imgs[i] + ".gif"); } Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597728 Share on other sites More sharing options...
next Posted July 23, 2008 Author Share Posted July 23, 2008 Thanks, i'll use that method. I finally figured it out, i had few errors: 1) i had <script type="javascript"> when it should have been <script language="javascript"> 2) Instead of getting array size like this: "imgNames.length" i used "imgNames.size". Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597753 Share on other sites More sharing options...
corbin Posted July 23, 2008 Share Posted July 23, 2008 Doesn't window.onload include images? Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597765 Share on other sites More sharing options...
KevinM1 Posted July 23, 2008 Share Posted July 23, 2008 Doesn't window.onload include images? I'm afraid I'm not quite sure what you're asking. ??? Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597819 Share on other sites More sharing options...
next Posted July 23, 2008 Author Share Posted July 23, 2008 i think he is saying that window.onload is fired after images are already displayed. Preload is done for hover effects. Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597872 Share on other sites More sharing options...
KevinM1 Posted July 23, 2008 Share Posted July 23, 2008 i think he is saying that window.onload is fired after images are already displayed. Preload is done for hover effects. Ah, good point. But, to be honest, hardly anyone uses JavaScript for hyperlink hover effects any more anyway. If it's a bunch of links, just use CSS :hover to do it. Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-597890 Share on other sites More sharing options...
next Posted July 23, 2008 Author Share Posted July 23, 2008 I do use CSS, but images are not cached untill i hover at least once which might result in a delay, that's why i cache them with JS when page is first loaded. Quote Link to comment https://forums.phpfreaks.com/topic/116209-js-preload-doesnt-seem-to-work/#findComment-598075 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.