SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Determine if HTML tag exists

    I'm using SWFObject for some flash components. I'm trying to interact with the embedded element. The problem is that I need to get either the object or embed element that SWFObject uses depending upon the browser.

    I tried the following but Firefox returns an object regardless,whether there are object element tags or not.

    Code:
    if(tags = document.getElementsByTagName("object"))
    		tags = document.getElementsByTagName("embed");
    Also, something to consider is that there are more than one embed or object elements, so they all have unique IDs.

    Is there a cross browser method to determine if a particular HTML tag exists? Or, am I approaching this wrong?
    -Lewis

  2. #2
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if(tags = document.getElementsByTagName("object"))
    do you mean
    if(tags == document.getElementsByTagName("object"))

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Lewis View Post
    The problem is that I need to get either the object or embed element that SWFObject uses depending upon the browser.

    I tried the following but Firefox returns an object regardless,whether there are object element tags or not.

    Code:
    if(tags = document.getElementsByTagName("object"))
    		tags = document.getElementsByTagName("embed");
    That's because you're not testing if there are objects. Instead you are testing for whether Javascript was able to assign a value to the tags variable.

    The getElementsByTagName method returns an array, even if none were found. Under these circumstances then the test will always be true, because it was able to assign something to the tags variable.

    It's best to avoid assignment expressions, as the previous poster demonstrated perfectly that confusion is very easy to achieve with that type of technique.

    Douglas Crockford has a nice piece about the Elements of JavaScript Style.

    Quote Originally Posted by Lewis View Post
    Also, something to consider is that there are more than one embed or object elements, so they all have unique IDs.

    Is there a cross browser method to determine if a particular HTML tag exists? Or, am I approaching this wrong?
    Code Javascript:
    var tags = document.getElementsByTagName("embed");
    if (tags.length > 0) {
        // do something with tags[0] and so forth
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2003
    Location
    Boston
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    That's because you're not testing if there are objects. Instead you are testing for whether Javascript was able to assign a value to the tags variable.

    The getElementsByTagName method returns an array, even if none were found.
    Thanks! I have a tendency to test and assign a variable like that in PHP. Testing the array overcomes the differences among browsers. It simply alluded me. Based on your example, I'm using the following:

    Code Javascript:
    var tags;
    tags = document.getElementsByTagName("object");
     
    if(0 == tags.length)
    {
    	tags = document.getElementsByTagName("embed");
    }

    Works like a charm. Thank you!
    -Lewis


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •