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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
thread Thread Starter Forum Replies Last Post
Quick Look help, after many googles rezwits Mac Basics and Help 7 Jan 5, 2011 09:16 PM
Need some quick javascript help... JustGretchen Web Design and Development 3 Feb 9, 2009 10:00 PM
Some quick Javascript help? MichaelThomas Web Design and Development 4 Jun 23, 2007 03:19 PM
Quick Javascript Help Grimace Design and Graphics 3 Aug 22, 2006 11:58 AM

All times are GMT -5. The time now is 05:46 AM.

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

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