Adding Border to Images

I am completely new to JS. I'm trying to make me a bookmarklet that finds all the images on a web page and adds a colorful border to them. Then, by clicking on the image, I would like to add a path to the image. This is what I have so far:

javascript:
for (var i= document.links.length; i-->0;) {
    if (document.links[i].getElementsByTagName('img').length!=0) {
        document.links[i].onclick= function() {
           window.open("http://www.example.com/whatever?imgsrc=" + this.src + "");
        };
    }
}

How to add border to images?

Thanks Bob

+5
source share
2 answers

Try this code:

javascript:for(i=0;i<document.getElementsByTagName('img').length;i++){var imgTag=document.getElementsByTagName('img')[i];imgTag.style.border='2px solid #E8272C';imgTag.onclick=function(){return !window.open(this.src)};}void(0)

Friendly format:

javascript:
for(i=0;i<document.getElementsByTagName('img').length;i++){
    var imgTag=document.getElementsByTagName('img')[i];
    imgTag.style.border='2px solid #E8272C';
    imgTag.onclick=function(){
        return !window.open(this.src);
    }
}void(0)
+1
source

No need to call getElementsByTagName

javascript:(function(){for(var i=0;i<document.images.length;i++){var image=document.images[i];image.style.border='medium solid blue';image.onclick=function(){location.href=this.src;return false;};}})()
0
source

All Articles