Stupid Javascript Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 2MuchMark
    Mark of 2Much.net
    • Aug 2004
    • 50977

    #1

    Stupid Javascript Question

    Greetings earthlings,

    I have a program that generates the URL of am image that looks like this

    http://something.jpg

    and it appears in the img code like this:

    <img src = "http://something.jpg">


    What I need is a javascript that will insert a 1 into the file name so that the URL looks like this

    http://1something.jpg"

    so that the code looks like

    <img src = "<script>blahblah</script>">


    STUPID question I know, but I just can't seem to figure this out. Any help gets you titties. thanks!
  • iceboi
    Confirmed User
    • Oct 2017
    • 302

    #2
    Code:
    let images  = document.getElementsByTagName('img');
    
    for (i=0;i<images.length;i++){
     let imageSrc = images[i].src.replace("http://","http://1");
    images[i].src = imageSrc;
    }
    This code will modify all the images on the page. If you only want to modify 1 image, give the image an id and change "getElementsByTagName" to "getElementById" and replace "img" with the id of the image.

    And don't put the script tags withing the img tag. It should be separate as it's own tag.
    Highest CPM pop-under |
    Best Converting Modern Cam Site

    Comment

    • freecartoonporn
      Confirmed User
      • Jan 2012
      • 7683

      #3
      no question is stupid question.
      SSD Cloud Server, VPS Server, Simple Cloud Hosting | DigitalOcean

      Comment

      • lezinterracial
        Confirmed User
        • Jul 2012
        • 3117

        #4
        never mind what I said before.
        Live Sex Shows

        Comment

        • Spunky
          I need a beer
          • Jun 2002
          • 133986

          #5
          It will never work

          Comment

          • 2MuchMark
            Mark of 2Much.net
            • Aug 2004
            • 50977

            #6
            Hi ICebol,

            Sorry but this didn't work for me. But I didn't include enough detail. Here's more:

            I have some HTML code that looks like this:

            *Start Loop*
            <img src="<!--Image-->">
            *End Loop*


            Where the value of <!--image--> looks something like this:


            Code:
            http://1.1.1.1/path/imagename.jpg
            So what I want to do is have a javascript that runs within this loop, and inserts a character at a specific spot of that URL.

            Does that help?


            Or put another way:

            *Start Loop*
            *Load Image URL*
            *Javascript find character N and insert X after N*
            <img src = "use new URL to display image">
            *end Loop*

            Comment

            • CurrentlySober
              Too lazy to wipe my ass
              • Aug 2002
              • 38944

              #7
              Originally posted by freecartoonporn
              no question is stupid question.
              Unless I am the one asking it...


              👁️ 👍️ 💩

              Comment

              • sarettah
                see you later, I'm gone
                • Oct 2002
                • 14297

                #8
                Originally posted by 2MuchMark

                *Start Loop*
                *Load Image URL*
                *Javascript find character N and insert X after N*
                <img src = "use new URL to display image">
                *end Loop*
                That is not a loop. That is a straight line. A loop repeats.

                Now, what are you trying to accomplish, beyond what you have already said?

                What triggers this to happen?

                So, we have a page and when the page first comes up, there is an image.

                On some trigger (unknown at this time) you want the image name to be edited on the fly to another image (image).

                Am I on the right track?


                edited in: This code: so that the code looks like

                <img src = "<script>blahblah</script>">

                Is not the way to implement anything. The only time that img tag is looked at would be at page load. aSince the script would be an invalid img ref it would just error out.

                The code that was posted in response would have to be run at the end of page load or when something triggered it (as I describe above).


                .
                All cookies cleared!

                Comment

                • 2MuchMark
                  Mark of 2Much.net
                  • Aug 2004
                  • 50977

                  #9
                  Originally posted by sarettah
                  That is not a loop. That is a straight line. A loop repeats.

                  Now, what are you trying to accomplish, beyond what you have already said?

                  What triggers this to happen?

                  So, we have a page and when the page first comes up, there is an image.

                  On some trigger (unknown at this time) you want the image name to be edited on the fly to another image (image).

                  Am I on the right track?


                  edited in: This code: so that the code looks like

                  <img src = "<script>blahblah</script>">

                  Is not the way to implement anything. The only time that img tag is looked at would be at page load. aSince the script would be an invalid img ref it would just error out.

                  The code that was posted in response would have to be run at the end of page load or when something triggered it (as I describe above).


                  .
                  Hi Sarah,

                  I will clarify:

                  It is a loop because it is loading a bunch of pictures, one after another. The program doing it is outputting to HTML. Unfortunately I can't edit that program at the moment, so I'm looking to edit the HTML side instead.

                  The string that the code generates looks like this:

                  Code:
                  http://1.1.1.1/images/picture1.jpg
                  And if I place that string in an IMG tag it loads the image. Fine.

                  But what I want to do is use some javascript to alter that string before placing it in the image tag.

                  Does that make sense? Or am I losing my marbles?

                  Comment

                  • freecartoonporn
                    Confirmed User
                    • Jan 2012
                    • 7683

                    #10
                    //get all images

                    //loop through all images src value

                    //change src value to new value.

                    //end.
                    SSD Cloud Server, VPS Server, Simple Cloud Hosting | DigitalOcean

                    Comment

                    • 2MuchMark
                      Mark of 2Much.net
                      • Aug 2004
                      • 50977

                      #11
                      Originally posted by freecartoonporn
                      //get all images

                      //loop through all images src value

                      //change src value to new value.

                      //end.
                      Yes, almost. Change src value WITHIN the loop to new value. repeat, then end.

                      Comment

                      • blackmonsters
                        Making PHP work
                        • Nov 2002
                        • 20961

                        #12
                        Originally posted by 2MuchMark
                        Greetings earthlings,

                        I have a program that generates the URL of am image that looks like this

                        http://something.jpg

                        and it appears in the img code like this:

                        <img src = "http://something.jpg">


                        What I need is a javascript that will insert a 1 into the file name so that the URL looks like this

                        http://1something.jpg"

                        so that the code looks like

                        <img src = "<script>blahblah</script>">


                        STUPID question I know, but I just can't seem to figure this out. Any help gets you titties. thanks!

                        Code:
                        
                        <script>
                        
                         	document.addEventListener("DOMContentLoaded", function(event) {
                           		var imgs = document.getElementsByTagName("img");
                        		var ctr = imgs.length;
                        		var i;
                        		for (i = 0; i < ctr; i++) { 
                        
                        			hold = imgs[i].src;
                        			imgs[i].src = hold.replace("https://","https://1");
                        		}
                        	});
                        
                        
                        </script>


                        ** Proudly accepting spare change at paypal to get account out of negative.


                        Free Open Source Live Aggregated Cams Script (FOSLACS)

                        Comment

                        • sarettah
                          see you later, I'm gone
                          • Oct 2002
                          • 14297

                          #13
                          Originally posted by 2MuchMark
                          Hi Sarah,

                          I will clarify:

                          It is a loop because it is loading a bunch of pictures, one after another. The program doing it is outputting to HTML. Unfortunately I can't edit that program at the moment, so I'm looking to edit the HTML side instead.
                          First, it is not sarah. You should know better.

                          Ok, but that is different then what you said originally.

                          How long do you want each image to appear for?

                          What you are describing is basically a slideshow and there are many scripts available to do what you want.

                          Again, what is the trigger? Do you just want it to loop through the whole time the page is being displayed, so from when the page loads to when the user leaves the page? Or is there some other trigger, user clicking something or anything like that?

                          Originally posted by blackmonsters
                          Code:
                          
                          <script>
                          
                           	document.addEventListener("DOMContentLoaded", function(event) {
                             		var imgs = document.getElementsByTagName("img");
                          		var ctr = imgs.length;
                          		var i;
                          		for (i = 0; i < ctr; i++) { 
                          
                          			hold = imgs[i].src;
                          			imgs[i].src = hold.replace("https://","https://1");
                          		}
                          	});
                          
                          
                          </script>
                          That would be close to what he wants except you are assuming that the images are already on the page.

                          .
                          All cookies cleared!

                          Comment

                          • sarettah
                            see you later, I'm gone
                            • Oct 2002
                            • 14297

                            #14
                            Demo at http://www.madspiders.com/imagetest.htm

                            Code used in the page:
                            Code:
                              <script type="text/javascript">
                              var imagecnt=1;
                              var maximage=9;
                              var timeout=1000;
                            
                              var imagename='testimages/carli_banks_1_000';
                              var imageid='myimageid';
                            
                              function startit()
                              {
                                var run=window.setInterval(changeimage,timeout);
                              }
                              
                              function changeimage()
                              {
                                imagecnt++;;
                                if(imagecnt>1)
                                {
                                  document.getElementById(imageid).src=imagename + imagecnt.toString() + '.jpg';  
                                }
                                if(imagecnt>=maximage)
                                {
                                  imagecnt=1;
                                }
                              }
                              </script>
                              </head>
                              <body>
                            
                              <img src="testimages/carli_banks_1_0001.jpg" id="myimageid" onClick="changeimage();" style="max-width:600;max-height:600;">
                              <br>
                              
                              <script>
                              startit();
                              </script>
                            
                              </body>

                            This is using a counter at the end of the base image name. You could have it insert into the imagename wherever you want it inserted. Since I don't know exactly where you want it inserted I just took the easy way out to show the concept.

                            Is that kind of what you are trying to do?

                            .
                            All cookies cleared!

                            Comment

                            • iceboi
                              Confirmed User
                              • Oct 2017
                              • 302

                              #15
                              Is this on the server side or client side? Are you using node.js?
                              Highest CPM pop-under |
                              Best Converting Modern Cam Site

                              Comment

                              • blackmonsters
                                Making PHP work
                                • Nov 2002
                                • 20961

                                #16
                                Originally posted by sarettah
                                That would be close to what he wants except you are assuming that the images are already on the page.

                                .
                                He said "The program doing it is outputting to HTML".
                                But he cannot edit the program so the images src has to be changed after the program
                                put out the html; which means the images are already on the page (based on his statement)

                                Javascript only executes on the html page DOM, so it's a given that the image src must already be part of the DOM.
                                Free Open Source Live Aggregated Cams Script (FOSLACS)

                                Comment

                                • sarettah
                                  see you later, I'm gone
                                  • Oct 2002
                                  • 14297

                                  #17
                                  Originally posted by blackmonsters
                                  He said "The program doing it is outputting to HTML".
                                  But he cannot edit the program so the images src has to be changed after the program
                                  put out the html; which means the images are already on the page (based on his statement)

                                  Javascript only executes on the html page DOM, so it's a given that the image src must already be part of the DOM.
                                  That is not what I got from his description.

                                  Either way, he says he wants to modify the image url that the program is producing. I see nothing that says the image names are available in the dom.

                                  When/if Mark comes back he can clarify. What I got from it was he wanted to do a slideshow with the urls for the images being produced dynamically based on some formula that he has not revealed.

                                  This thread is a really good example of a piss poor project description. ;p

                                  imho. of course.

                                  .
                                  All cookies cleared!

                                  Comment

                                  Working...