2

Im using the MVC 2 framwork and have added some javascript for expanding divs. It works fine in firefox, chrome, opera and safari but not in internet explorer. I get an 'Object Expected' Error. Here is my code

the jquery import is in the site.master file

 <head runat="server">
<title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />   
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" charset="utf-8"></script>

The Javascript, in the mvc view: the test alert comes up in IE but test2 doesn't.

   <script type="text/javascript">
    alert("test");

    $(document).ready(function () {
        alert("test2");
        $(".expandingcontent").hide();

        $(".divexpand").click(function () {
            var divID = "#" + $(this).attr("id").substring(6);
            if ($(divID).is(":hidden")) {
                $(divID).slideDown("slow");
            } else {
                $(divID).hide();
            }
        });
    });
</script>     

Ive tried placing the javavscript at the beging of the page, at the end nothing seems to work. Ive also tried using a timeout but no success there either. I'm using IE 8, any help is very much appreciated Thanks!

WillMcKill
  • 391
  • 2
  • 6
  • 13

4 Answers4

3

Your reference to Google CDN resource is wrong, especially if your site is using SSL.
Change your reference like this and it should work.

<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>

Its because,

It’s not exactly light reading, but section 4.2 of RFC 3986 provides for fully qualified URLs that omit protocol (the HTTP or HTTPS) altogether. When a URL’s protocol is omitted, the browser uses the underlying document’s protocol instead.

Dave Ward explains it more: Cripple the Google CDN’s caching with a single character

P.S: Its better to use Google CDN, together with a local fallback resource in case CDN fails to load

Community
  • 1
  • 1
naveen
  • 53,448
  • 46
  • 161
  • 251
2

It might be caused by it not being able to load the jquery.js? Try downloading it and putting it as a local resource.

Yngve B-Nilsen
  • 9,606
  • 2
  • 36
  • 50
  • Agreed. It works fine on my machine as a local resource. The jquery.js file is probably not fully downloaded by the time IE tries to run the $(document).ready function. – tobias86 Mar 25 '11 at 09:46
  • Yep thats it, d/l Jquery and used as a local resource has fixed my problem. Anyone think the issue importing jquery from the url was caused because my site is using ssl? http://stackoverflow.com/questions/1056497/simple-jquery-code-works-fine-until-site-is-loaded-via-https – WillMcKill Mar 25 '11 at 09:47
  • @Will yes it's very likely - by default browser should alert when `https` website try to load resources from non secure sites. When it's JS files, the browser probably simply won't download it. – Shadow The GPT Wizard Mar 25 '11 at 11:01
1

In my case, I had an extraneous console.log in the JS and it quietly broke in IE. It was confusing because when I enabled the IE developer tools it magically started working. Anyway, check that, too.

Dan Caddigan
  • 1,578
  • 14
  • 25
1

Make sure to include the jquery script file before you execute the $(document).ready function.

tobias86
  • 4,979
  • 1
  • 21
  • 30