tags:

views:

72

answers:

2

How do I add adsense code to this javascript code. I think there might be a solution with jquery and getscript, but I'm not able to get it to work

<script>
var rep = $("#topbar").find(".reputation-score").text().replace(/,|\s/g,'');
if (rep.match(/\d+k/i)) rep = rep.replace(/k/g, '') * 1000;
if (rep == "") rep = 0;
if (rep < 200 && $("#banner-bottom").length == 0) {
  if ($(".answer").length == 0)
    abovebanner = $("#answers-header");
  else
    abovebanner = $(".answer:first");
  abovebanner.after('HELP ADD ADSENSE CODE HERE');
}
</script>

and guys I'm not very good with javascript, so if you guys could paste a working solution. I'd really appreciate it.

A: 

I'm not sure I understand your question, but you would just insert it into the string. Maybe with concatenation:

code = "<script language='javascript'>code goes here</script>";

(...).after('<div class="hole">'+code+'</div>');
Pekka
that doesn't get executed, basically I'm looking for a way to call a seperate javascript file that'll have the adsense advance code in it.
blah
+2  A: 

This page explains a method to do this. Once you have created the wrapper function (addJavascript) mentioned on that page added, your last line would like something like:

addJavascript('adsense-library-name.js','head');

The implementation of addJavascript (from the page above):

function addJavascript(jsname,pos) {
  var th = document.getElementsByTagName(pos)[0];
  var s = document.createElement('script');
  s.setAttribute('type','text/javascript');
  s.setAttribute('src',jsname);
  th.appendChild(s);
}
ghills
Please provide an answer here, in part at the very least.
Jonathan Sampson
@Jonathan Sampson: Who do you mean, the OP?
Pekka