Solved

jQuery chokes on a page without doctype .dtd file reference

Posted on 2011-03-15
2
507 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Read about why website design really matters in today's demanding market.
Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now