Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AWShuttleworth

macrumors newbie
Original poster
Jul 1, 2007
20
0
I'm trying to use JS to check if a string is on a page, and if it is, to inject some HTML into a DIV on the page. Here's my code—what stupid mistake am I making?

Code:
<script type="text/javascript">
  if (document.body.innerHTML.indexOf('My String'))
  	{
  	document.getElementById('target').innerHTML = 'Hello, world!';
  	}
</script>
<div id="target"></div>
 
Where are you running this script? It is only likely to work if you run it after the body has loaded so either attach it using the body onload event or if you are using jquery or the like use document.ready.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.