dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Help with JavaScript Code

  1. #1
    Join Date
    Mar 2006
    Posts
    228

    Question Help with JavaScript Code

    Hello,

    I have the following JavaScript:

    Code:
    function reloadIt1() 
      { 
    var image1 = (document.getElementById("img0").src);
    image1 = image1.split('?', 2); 
    
    
      if (document.getElementById("img0")) 
            { 
            document.getElementById("img0").src = (image1[0]) +"?"+ Math.random();
    
            
            setTimeout("reloadIt1()", 1000); 
           ;
            } 
       else 
            { 
            setTimeout("reloadIt1()", 1000); 
            } 
      } 
       { 
    
    }
    This code works fine.. however, if the .src has a ? (question mark) in the URL it don't work, as it is needed to split the URL to remove the random number it creates.

    I has to create a random number to the end, otherwise it won't refresh the image as it will display the cache image and won't get a new image.

    So my question is, how can I get the image to refresh with a new image (based on it's orignal src)? Somehow I need a way to remove the random number it creates and replaces it with a new one, without filtering a ? (question mark) to get the random number.

    Anyone got any ideas?

    Thanks.

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Help with JavaScript Code

    Your code shows some major syntax errors and logistical/efficiency errors in its implementation.

    PHP Code:
    function reloadIt1() { 
      
    // check for the DOM element right away
      
    if (document.getElementById("img0")) { 
        
    // extract the src
        
    var src document.getElementById("img0").src;

        
    // check for a question mark...if there, split it out
        
    if (src.search("?") > -1) {
          var 
    parts src.split('?');
          
    src parts[0];
        }

        
    // update the src
        
    document.getElementById("img0").src src +"?"Math.random();
        
    // reload
        
    setTimeout("reloadIt1()"1000); 
      } 

    Also, parenthesis go around mathematical equations for proper equation ordering. You're using them for strings here.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Mar 2006
    Posts
    228

    Re: Help with JavaScript Code

    Hello,

    Thanks for your reply.

    However, this does not work if you have a ? (question mark) in the src.

    For Example:
    If the src = http://123.123.123.123/image.get?image=1
    then it would not work as the ? (question mark) is already in the URL already, however if the ? (question mark) wasn't in the src URL already then it would work but my image im am trying to refresh has a ? (question mark) in the URL.

    Is there a way of just removing the last random number from the string and then add the new random number? (remove the last 19 numbers from the string?)

    Hope someone can help.

  4. #4
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Help with JavaScript Code

    First off, I never said it was the solution to your problem. I didn't understand your English in the first post. To be completely honest, I still don't understand your problem. Give a clear example of a URL and a clear example of to what it needs to be changed.

    If you're having problems with a question mark, as you can see, the code provided does split it. Then just take the part your need and put it back into your string.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  5. #5
    Join Date
    Mar 2006
    Posts
    228

    Question Re: Help with JavaScript Code

    Sorry about if you don't understand.. me explain it a little better.

    I have a Image on a page:

    Code:
    <img src="http://123.123.123.123/camera.get?image=1" alt="web image" id="img0" />
    I need to refresh that image every second (as it will change - it's a camera).

    however, I need to refresh that image without displaying the old image thats why I am adding a random number to the end of the image. (so it Forces a new image)

    however, what I am facing is that when I refresh the image with my code

    Code:
    function reloadIt1() 
      { 
    var image1 = (document.getElementById("img0").src);
    image1 = image1.split('?', 2); 
    
    
      if (document.getElementById("img0")) 
            { 
            document.getElementById("img0").src = (image1[0]) +"?"+ Math.random();
    
            
            setTimeout("reloadIt1()", 1000); 
           ;
            } 
       else 
            { 
            setTimeout("reloadIt1()", 1000); 
            } 
      } 
       { 
    
    }
    It replaces the image (just like it's meant to, with a random number) however the URL of the image source is just getting bigger and bigger as it keeps adding a random number onto the end without replacing the random number.

    that is what I am needing help with.

    I need to refresh the image with a new random number.

    What I want to happen:

    Orignal URL = http://123.123.123.123/camera.get?image=1
    new URL = http://123.123.123.123/camera.get?image=1?0.5694605531825758
    1 second later, URL = http://123.123.123.123/camera.get?image=1?0.2356246538895740
    2 second later, URL = http://123.123.123.123/camera.get?image=1?0.4336847536855723
    etc...


    at the minute its doing the following:

    Orignal URL = http://123.123.123.123/camera.get?image=1
    new URL = http://123.123.123.123/camera.get?image=1?0.5694605531825758
    1 second later, URL = http://123.123.123.123/camera.get?image=1?0.5694605531825758?0.2356246538895740
    2 second later, URL = http://123.123.123.123/camera.get?image=1?0.5694605531825758?0.2356246538895740?0.4336847536855723
    etc...


    I can't seem to work out how to replace the old random number with a new random number.

    Got any ideas now ?

    Hope this helps now..

  6. #6
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Help with JavaScript Code

    First off, I already told you that your code has multiple errors in it. Don't use it as written.

    Second, why are you making this so hard on yourself. Since you already know the URL root...just add a new number everytime.

    You're making this way too hard on yourself!

    Code:
    function reloadIt() {
      if (document.getElementById("img0")) {
        document.getElementById("img0").src = http://123.123.123.123/camera.get?image= + Math.random();
        setTimeout("reloadIt()", 1000);
      }
    }
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  7. #7
    Join Date
    Mar 2006
    Posts
    228

    Re: Help with JavaScript Code

    Hello,

    Thanks for you reply..

    I need to do this the hard way as a computer program creates this main page and my .js file (JavaScript) needs to control it.

    The User can change this from a GUI and this JavaScript needs to find that URL (in the background) and refresh it.

    This URL could change based on the URL the user types in.

    The only way I can do this is to use code only and I have no idea what the URL is when the page is created.

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    Re: Help with JavaScript Code

    So use the code I just gave you and parse the URL yourself! It's not that hard to split the string, grab the first half, add the "?image=" and random number.
    Last edited by PeejAvery; March 30th, 2011 at 12:46 PM.
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)