Jump to content

Image() note.


mrbean

Recommended Posts

I have made an loop for images to make them an javascript image object.

Just like

image[1]

image[2]

 

They al have assigned .src attribute.

But the thing is I don't know which image what is so I want to refer to the image name.

I want to assign a name for every image in the loop.

Just like I have a list of names ordered by numbers

so the value of imgname[1] must be commented out in image[1]

So i can refference by the comment.

 

I hope you understand what I am trying to say.

And sorry for my bad english.

Link to comment
Share on other sites

I really don't have a clue, as to what your looking for; but since no one else responded to you and because I am bored, lol... I am going to try to give it a shot.

 

Try something like this; maybe it will get you started:

 

<script>

var created, created2 = "no";

function getImage(imgName)

{

var image1 = new Image();
image1.setAttribute("id","Photo 1");
image1.setAttribute("src","pic1.jpg");
image1.setAttribute("width","1");
image1.setAttribute("height","1");

var image2 = new Image();
image2.setAttribute("id","Photo 2");
image2.setAttribute("src","pic2.jpg");
image2.setAttribute("width","1");
image2.setAttribute("height","1");

// validation

if (imgName == "Photo 1") 
{
// insert image only once into page
  if (created != "yes")
   {
   document.body.appendChild(image1);
   created = "yes";
   }
  var imgSRC = document.getElementById(""+imgName+"").src;
  alert(imgSRC); // for example purposes
}
else if (imgName == "Photo 2")
{
// insert image only once into page
  if (created2 != "yes") {
   document.body.appendChild(image2);
   created2 = "yes";
  }
  var imgSRC = document.getElementById(""+imgName+"").src;
  alert(imgSRC); // for example purposes
}

}

</script>


<button onclick="getImage('Photo 1')">See The SRC For "Photo 1"</button>
<button onclick="getImage('Photo 2')">See The SRC For "Photo 2"</button>

 

If you play around with it some; I am sure you can cut down on some of the code recursion, but maybe this will help you out... at least for demo purposes.

 

If you have some code to post; that will probably help people to understand what you trying to do and will, in turn... get more responses to your question.

Link to comment
Share on other sites

-snap-

Thanks for your reply, but let me explain this again with some code examples and now more properly.

 

var image = new Array();
var i = 0;
while (i<5)
  {
       image[i] = new Image();
       image[i].src = "test0"+i+".png";
       i++;
  }

// So now I have a list of images.
// What if I want to choose the image with source: test01.png
// but by reffering an assigned name.

an example from mysql syntax:

Select image.src from image where image.something = happysmiley.

Like I have an list of images but one has something where I can refer to to find that specific image. Reffering by name of the image for example.

Link to comment
Share on other sites

If you know that you want image 01, and you have created the array with image, then you already know the index you want: Image01 == index 1.

I want to refer by an assigned name to it, not counting to finally find the image and say i want image[1323] or something like that, it wil cost alot of time.

Link to comment
Share on other sites

What you're asking to do doesn't make any sense. Why would you want to refer to the image by it's src of test01.png, when you know you can access it through image[1]? Sounds like you're trying to over complicate things.

 

If in your actual code you're not using numeric values like that though, you could just use a standard object instead:

 

function createImage(src) {
    var image = new Image();
    image.src = src;
    return image;
}

var images = {
    '/path/to/foo.png': createImage('/path/to/foo.png'),
    '/path/to/bar.png': createImage('/path/to/bar.png'),
    '/path/to/baz.png': createImage('/path/to/baz.png')
}

console.log(images);
console.log(images['/path/to/foo.png']);
console.log(images['/path/to/bar.png']);
console.log(images['/path/to/baz.png']);

 

http://jsfiddle.net/UQHey/

 

If that is the case though you should have posted your code, or mentioned you're not using sequential naming like that.

Link to comment
Share on other sites

What you're asking to do doesn't make any sense. Why would you want to refer to the image by it's src of test01.png, when you know you can access it through image[1]? Sounds like you're trying to over complicate things.

 

I am not asking that, but it's already fixed :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.