-1

Hello my question is how to write inside script called parent element with document.write when parent element is unknown

You may consider this as advertising script

For example:

<div>
<script src="http://www.pokemonpets.com/scripts/ads_simple.js"></script>
</div>

My script code below but not working right now

document.write('<a title="Bedava Pokemon Online Oyunu" target="_blank" href="http://www.pokemonpets.com/Register"><img src="http://orig04.deviantart.net/58d0/f/2015/213/8/4/pokemonpets_by_monstermmorpg-d93plr1.png" /></a>');

So somehow i have to make it work without knowing parent element or without knowing whether page has JQuery or not.

How does advertising companies handle this?

MonsterMMORPG
  • 20,310
  • 69
  • 183
  • 306

1 Answers1

0

You don't want to use document.write, you want to find your script tag and replace it with your new content. You can do that by looking for a script tag with your URL, then having it's parent replace it.

scripts = document.getElementsByTagName("script");
for(var i in scripts){
   if(scripts[i].src.indexOf('//www.pokemonpets.com/scripts/ads_simple.js') !== -1){
      var wrapper = document.createElement('div');
      wrapper.innerHTML = '<a title="Bedava Pokemon Online Oyunu" target="_blank" href="http://www.pokemonpets.com/Register"><img src="http://orig04.deviantart.net/58d0/f/2015/213/8/4/pokemonpets_by_monstermmorpg-d93plr1.png" /></a>';
      scripts[i].parentElement.replaceChild(wrapper,scripts[i]);
   }
}

This is totally untested, not even for syntax, so you might need to fiddle with it a bit.

trex005
  • 4,637
  • 1
  • 22
  • 39