Solved

jQuery chokes on a page without doctype .dtd file reference

Posted on 2011-03-15
2
509 Views
Last Modified: 2012-05-11
Hello everyone.
I have a little piece of jquery running... i found it chokes on IE.. IE always renders the "else"
Firefox and Chrome work perfect.

If you duplicate this code in 2 files and then put the doc type for one as
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
 
and the doc type for the second as
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

The rest of the file is the same.
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

</head>
<div id="rightFloat" style="width:200px;border:thin solid pink;">
HEY I Am Here!
</div>
  <img src="justj.jpg" />
    <script language="javascript">
        if ($('img[src = "justj.jpg"]').length) {
            //if the image with that src exists
            document.getElementById('rightFloat').style.display = "block";
                  document.write("Yippeee.. I display block!")
        } else {
            document.getElementById('rightFloat').style.display = "none";
                  document.write("I am the else statement, I display none!")
        }
</script>
</html>

You notice that in firefox and chrome - BOTH WORK and the condition pops up the div.
You'll notice in IE, that the one without the .dtd reference in the doc type does NOT WORK

If I can not change the doc type.. is there anything else to consider why IE would choke on this particular code snippet and not other instances of jquery?
0
Comment
Question by:jmcreative
2 Comments
 
LVL 19

Accepted Solution

by:
Albert Van Halen earned 500 total points
ID: 35142648
Try and use the ends-with selector. See http://api.jquery.com/attribute-ends-with-selector/
Perhaps also check for length > 0. Because it returns the jquery object which always have a length property. Length being the number of elements matching the criteria.
if ($('img[src$="justj.jpg"]').length > 0)

Open in new window

0
 

Author Closing Comment

by:jmcreative
ID: 35151659
Excellent. That did work.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question