changing/stripping off url pointed by image using javascript -
changing/stripping off url pointed by image using javascript -
might simple javascript injection question, have image html tag:
<a href="myfile.htm"><img src="rainbow.gif"></a> i wanted perform javascript, such when clicked on image, doesn't go myfile.htm. in other words, wanted strip href surrounds img. how can in javascript? have next reference image tag:
document.elementfrompoint(%f, %f) f can replaced double/float value
if have reference img element, parent (parentnode) link (in construction you've given). 3 options:
href 1. remove link entirely you can remove link exclusively doing this:
var link = img.parentnode, linkparent = link.parentnode; linkparent.insertbefore(img, link); linkparent.removechild(link); that uses parentnode find parent , grandparent, insertbefore move image, , removechild remove link. note assumes image thing in link.
if want maintain link render useless, can this:
var link = img.parentnode; if (link.addeventlistener) { link.addeventlistener("click", function(event) { event.preventdefault(); }, false); } else if (link.attachevent) { link.attachevent("onclick", function() { homecoming false; }); } else { link.onclick = function() { homecoming false; } } 3. alter href of link: this trivial, set href property of link element (which can because it's parent node of image) whatever want:
img.parentnode.href = /* ...something else */; for instance:
img.parentnode.href = "http://stackoverflow.com"; ...would alter link point stack overflow.
live example
some references:
dom2 core dom2 html dom3 core html5 web application apis javascript
Comments
Post a Comment