6

I have this:

<span class="image"><img src="something.jpg"></span>

I want to transform it to that using javascript:

<span class="image"><a href="domain"><img src="something.jpg"></a></span>

It has to be done using javascript in order to hide the affiliate links.

I have tried this script but it seems not to work:

function changespan() {
find all <span> tags;
for each <span> with class="image"{
URL = "http://domain.com"
Create new link to URL;
insert link into <span>;
}       
}

The function is uploaded in file script.js and I load it in this fashion:

<script type="text/javascript" src="script.js"></script>
<script type="text/javascript">
window.onload = changespan;
</script>

EDIT: After this is solved, how could i parse my page to find links in this format: and then assign this value to variable URL. I need to be able to assign first path to URL_1 second to URL_2 and so on.

9
  • 1
    You seem to be missing this and that... Commented Mar 11, 2013 at 13:40
  • 2
    What scripting language is "find all <span> tags" WishfulThinking language? Commented Mar 11, 2013 at 13:41
  • 4
    Try <script type="pseudocode"> Commented Mar 11, 2013 at 13:42
  • 2
    There should be a language like that! I can see it now englishscript Commented Mar 11, 2013 at 13:42
  • 1
    Lol @TimGoodman - i named it differently ;) Commented Mar 11, 2013 at 13:42

2 Answers 2

9

This is how you can implement it:

function changespan() {
    var spans = document.querySelectorAll('span.image');
    for (var i = spans.length; i--; ) {
        var a = document.createElement('a');
        a.href = "http://domain.com";
        spans[i].appendChild(a).appendChild(a.previousSibling);
    }
}

http://jsfiddle.net/Tqv76/1/

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

7 Comments

I have never seen a for formatted like that. Does it exit because when i-- to 0 it is falsey?
@mplungjan Why should it duplicate the image? Inside the loop a will be always new one with the own image, see a is recreated in the loop. I see your confusion: this is just lorempixel.com caches requests and outputs the same image. I will update the demo to use different images.
Yes, of course it moves the image inside the link, this is what we need.
Why? Moving the nodes is much more efficient then recreating them. In fact appendChild is constructed to aim this goal: insert and move.
Thank you very much. Another thing I need to do is parse link from the page and use it as link.href (URL). The needed link is "link.html" from <a rel="nofollow" href="link.html" class="1" >. There is only one such link on the whole page.
|
4

Here, I translated it to JavaScript keeping your pseudo code as intact as possible

DEMO

window.onload=function() {
  var spans = document.getElementsByTagName("span"); // or the newer querySelectorAll
  for (var i=0;i<spans.length;i++) {
    if (spans[i].className=="image") {
      var link = document.createElement("a");
      link.href = "http://domain.com";
      link.setAttribute("rel","nofollow");
      link.className="someclass";
      link.innerHTML=spans[i].innerHTML;
      spans[i].replaceChild(link,spans[i].getElementsByTagName('img')[0]);
    }       
  }
}

5 Comments

Well the demo actually doesn't output clickable images. They should be clickable.
Please look again. I had a typo in the getElements - I left the <> around the span
Works now. Thank you very much. This code will be very usefull for many many people. Another thing i need to do is to parse link and use it as link.href (URL) - <a rel="nofollow" href="link.html" class="1" >
Added the class and rel, not sure what you mean by parse the link
Already solved - thanks (Using URL parsed from the page) - stackoverflow.com/questions/15341473/…

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.