Solved

jQuery chokes on a page without doctype .dtd file reference

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Because your company can’t afford for you to make SEO mistakes, you’ll want to ensure you’re taking the right steps each and every time you post a new piece of content. This list of optimization do’s and don’ts can help you become an SEO wizard.
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
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.

710 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