Solved

jQuery chokes on a page without doctype .dtd file reference

Posted on 2011-03-15
2
511 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

763 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