Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Thread Tools Search this Thread Display Modes
Old Nov 3, 2010, 11:47 AM   #1
macrumors newbie
Join Date: Jul 2007
Quick JavaScript help?

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?

<script type="text/javascript">
  if (document.body.innerHTML.indexOf('My String'))
  	document.getElementById('target').innerHTML = 'Hello, world!';
<div id="target"></div>
AWShuttleworth is offline   0 Reply With Quote
Old Nov 3, 2010, 12:58 PM   #2
macrumors regular
Join Date: Mar 2009
Location: Skipton, UK
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.
Dunmail is offline   0 Reply With Quote

MacRumors Forums > Special Interests > Visual Media > Web Design and Development

javascript html

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: Quick Reminder/Quick Note tweak? InsideBoxer Jailbreaks and iOS Hacks 3 Jan 29, 2014 04:22 PM
Need quick explanation of javascript code jonpeter Web Design and Development 9 Nov 10, 2013 06:32 AM
iOS 7 Concept: Quick Gestures, Fullscreen App Switching, and Quick Settings Cam1 iOS 7 14 May 28, 2013 10:33 AM
JavaScript keeps enabling itself nilesisniles iOS 6 1 Nov 27, 2012 04:40 PM
Safari 6.0.2 and javascript Franciturci OS X 10.8 Mountain Lion 2 Nov 16, 2012 09:32 AM

Forum Jump

All times are GMT -5. The time now is 11:29 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013,, LLC