Welcome Posted April 23, 2009 Share Posted April 23, 2009 Is it possible to link images with JavaScript? I've been trying to figure this out for the past two days.What I want is to get the source of an image and wrap the image with anchor tags linking to the image's URL (so that you can see the whole image, images on my site are often resized)? Something like this: <a href="imagepath"><img src="imagepath" alt="text" /></a> How would I go about doing this? And if it can not be done in JavaScript, do you know what it can be done in (PHP?)?EDIT: This is not like a random image script. In those scripts the JavaScript already knows the source of the image. But, I want to A ) search the whole document and find an image's source OR B ) add the function only to images I want to be linked and find its source.Am I making sense, or do I need to clarify something? Link to comment Share on other sites More sharing options...
Synook Posted April 23, 2009 Share Posted April 23, 2009 http://www.lokeshdhakar.com/projects/lightbox2/? Link to comment Share on other sites More sharing options...
Welcome Posted April 23, 2009 Author Share Posted April 23, 2009 Lightbox needs to already know the path of the file. I need something to figure out how to get it to find the path for me.btw I don't need someone to code it for me, just need some help on how to figure it out. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2009 Share Posted April 23, 2009 How do you expect to determine the source of an image? What information do you have available on the page to determine that? Link to comment Share on other sites More sharing options...
Welcome Posted April 23, 2009 Author Share Posted April 23, 2009 How do you expect to determine the source of an image? What information do you have available on the page to determine that?I already have the image on the site, just want to be able to link to it. I think I've almost got it though.Would using an onClick event work if I use this:window.open(this.src) I've tested it inline, I think it'll work if I add a function in the head (why shouldn't it). This seems fairly simple, I think I didn't explain this right. Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2009 Share Posted April 23, 2009 <img src="image1.jpg" onclick="window.open(this.src, '_blank');">That will set it up to open the image in a new window, if that's what you're looking for. Link to comment Share on other sites More sharing options...
Welcome Posted April 24, 2009 Author Share Posted April 24, 2009 <img src="image1.jpg" onclick="window.open(this.src, '_blank');">That will set it up to open the image in a new window, if that's what you're looking for.Yeah it was, thanks. Link to comment Share on other sites More sharing options...
Welcome Posted April 24, 2009 Author Share Posted April 24, 2009 What is wrong with my code?I have this in the head section: <script type="text/javacript"> function openImg(obj) { window.open(obj.src,'_blank'); }</script> And here is the image I'm trying to use it on: <img src="filepath" alt="alt txt" onClick="openImg(this)" /> It works fine when I don't use a function though. Oh and btw the error I get is "openImg is not defined" Link to comment Share on other sites More sharing options...
jesh Posted April 24, 2009 Share Posted April 24, 2009 What is wrong with my code?<script type="text/javacript"> function openImg(obj) { window.open(obj.src,'_blank'); }</script> text/javacript should be text/javascript. Link to comment Share on other sites More sharing options...
Welcome Posted April 25, 2009 Author Share Posted April 25, 2009 Ugh.. I'm always misspelling that. Thanks guys. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.