jquery "$(document).ready(function () {" isn't working in IE
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 javav开发者_Python百科script 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!
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
It might be caused by it not being able to load the jquery.js? Try downloading it and putting it as a local resource.
Make sure to include the jquery script file before you execute the $(document).ready function.
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.
精彩评论