thanks this worked well.  I guess the key the if statement is testing for truth
of a statement that anything that makes it false counts as false even if its
non existence.

o = document.getElementsByTagName('title')[0]) &&

nice code.  thanks

On 6/17/07, RobG <[EMAIL PROTECTED]> wrote:


On Jun 17, 2:52 pm, "Scottus " <[EMAIL PROTECTED]> wrote:
> I am using
>
> var title = document.getElementsByTagName('title').item(0).innerHTML;
>
> to get the content of a pages title tag.
>
> But if the page has no title tag I get
>
> "Error: document.getElementsByTagName("title").item(0) has no properties"
>
> and the script craps out.
>
> any ideas about how to deal with this ?

An HTML document without a title element is invalid.  Christopher has
given you a jQuery answer, a generic answer is that if you try to get
a property of an object that doesn't exist your script will error.  If
you want a belt & braces approach, try something like:

 var o;
 if ( document &&
      document.getElementsByTagName &&
      (o = document.getElementsByTagName('title')[0]) &&
      (typeof o.text == 'string') )
  {
    alert('The document title is: ' + o.title);
  }

Or if you like being a little more risque, try:

  alert( document.title );


--
Rob




--
   Scott Wickham

********************************************************************************************
Everyone is equal and everyone is the best at everything.  ---
Principal Skinner

"Success is a lousy teacher. It seduces smart people into thinking
they can't lose."       -Bill Gates

99% of the time, in my experience, the hard part about creativity
isn't coming up with something no one has ever thought of before. The
hard part is actually executing the thing you've thought of.  -- seth
godin


********************************************************************************************

Reply via email to