Loading images in the background

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NetWorker
    New Member
    • Aug 2010
    • 3

    Loading images in the background

    Hello everyone ;)

    I would like to load some images using JS.

    My code:
    Code:
    var img=new Image();
    img.onload = function() {f1()};
    img.onerror = function() {f2()};
    img.src='http://example.com/image.gif';
    sett = setTimeout(function() {f3()}, 100000);
    
    //f3 contains not interesting code and:
    img = null;
    clearTimeout(sett);
    Simply task, but I have two problems.

    1) Some images don't listen to orders from setTimeout and load for 200 and more second. What is wrong with my code? Notice, that it happens only with some images, not all.

    2) Images are loading according to status of currently user. In some cases there can appear images which require to login by htpassword to see them. Then user would see a window with fields to login. What can I do to in that case skip this image and avoid showing that window?

    Thank you so much for any help.
    Last edited by NetWorker; Aug 20 '10, 08:31 PM. Reason: Missing a letter
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    1) what do you mean with 'orders from setTimeout' exactly?

    2) i would say that you would need to handle that serverside - the client don't know - and shouldn't know - what the user is allowed to do. the serverside could request the photo from a directory and check the users permission - when it is a forbidden picture - just proceed to try to read the next one.

    Comment

    • NetWorker
      New Member
      • Aug 2010
      • 3

      #3
      Thank you so much for answer

      1) Never mind. I changed some things and it works perfectly.

      2) I can't do it server-side - that's the point. However, server does know which images are available for current user. If he request wrong image he gets 401 error. Privileges are given dynamicly, are diffrent according to user status, actual time etc. It must be JS.

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        when implementing security critical things clientside with JavaScript you would need to be aware that this could easily be tricked by medium skilled users - so in case it would be 'mission critical' you couldn't rely on such an implementation.

        Comment

        • NetWorker
          New Member
          • Aug 2010
          • 3

          #5
          Images are protected by 401 Error - user can't access them manipulating JS.

          My site is dynamicly changing, sets of images are changing during visit. My script is too complicated to be always right. Sometimes there can be a bug - requesting wrong image.

          Is there any way to ignore these images and go to next?

          (Be sure that I made all my best to solve this problem by another solution - this is my last chance)

          Comment

          Working...