0

I want to have an image in a webpage that can be changed based on the filepath stored in a file online (it doesn't matter what typer of text file - xml, .txt - whatever works best). So I basically want to have the page retrieve the text from that file, and then use that text as the source for an image in that page. I'm assuming this is a Javascript thing, but it doesn't matter to me, as long as it works. Any ideas? Thanks!!

**Edit: Forgot to mention: I'm using the code in a Google Chrome Extension, not sure that matters, as it uses regular HTML/Javascript, but it's stored on the users computer, and I want the image to be stored on my server.

**Edit2:

Just got something that seems to work very well, and I only need this in the body part of the code:

      <script type="text/javascript" >
      var i=0;
      for (i=0;i<=FilePath.length - 1;i++)
      {
      document.write('<img src="' + FilePath[i] + '"/>');
      }
      </script>

Hope this is valid code, but it definitely seems to work here...

5
  • 1
    Why do you just include it dynamically on the server side? This would generally be a much much better way of handling it. Commented May 22, 2011 at 2:39
  • Could be that makes more sense, I just don't know how to do what your suggesting... Commented May 22, 2011 at 2:46
  • Can you provide some more information/detail? Thanks! Commented May 22, 2011 at 2:46
  • I think he was referring to something like my answer below. Commented May 22, 2011 at 2:55
  • Aha, like I mentioned below, I don't think that will work for me, since I don't think I can use PHP... Commented May 22, 2011 at 2:55

2 Answers 2

1

Its simple store the filepaths in a Javascript file , create an array in the JS file , and include all the filepaths in the array, then store the file on the webserver .

Then after that you can retrieve it using

<script src="JS_File_path_on_web_server" type="text/javascript" ></script>

After you retrieve it , you can use Javascript , I prefer jQuery , to replace the src attribute on the Image with the one from the array .

EDIT : Full version :

//Javascript web server File
var FilePath=new Array("Path1","Path2","Path3");

Create a file like this and store as many paths as you want in the array .

<html>
 <head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js">
  </script>

  <script src="JS_File_path_on_web_server" type="text/javascript" ></script>

  <script type="text/javascript" >
   $(document).ready(function()
   {
      $('#DisplayImage').attr('src',FilePath[0]);
   });
  </script>
  </head>
  <body>
        <img src="" id="DisplayImage" />
  </body>
</html>

This is a simple example , you can try learning javascript and Jquery to tweak it further .

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the info! I don't really know Javascript or jQuery though - any chance you can help me with the rest of the code?
One more question: Is it possible to show a variable number of images depending on the number of paths contained in the array?
I think it is possible , just run a for loop for all the paths in the array and then you can probably use document.write() to create the images dynamically .
1

Since my php based answer was not applicable, we can all ignore it now. :)

5 Comments

Just added in above that I'm using the code in a Google Chrome Extension, and I don't think I can use a PHP function...
@IsaacL, right you are. In this case, Guatham's suggestions a good one. jQuery would be pretty simple for this. If he doesn't show you how, I could.
Thanks anyway for the help! Still looking for one more part of it, but I hope that code should help...
@IsaacL, your javascript for loop looks fine. ;-) Once you learn javascript fundamentals, jQuery is well worth studying. It really streamlines things, and compensates for differences between browsers. If you don't have the firebug extension for firefox, get it. jslint is good as a debugging last resort. Do all that, you'll be a javascript adept in no time.
Thanks a lot!! Sounds like a good idea, though I still need to figure out the difference between javascript and jQuery ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.