0

could someone please point out where I might be wrong? My problem is that in my views I have some scripts and it seems that jQuery just somehow does not work there (probably my fault somewhere).

I have reference to jQuery specified in my layout page that is being loaded always. Then I have my view that looks like this

@model jakubholovsky.com.Models.BlogPost
@using jakubholovsky.com.DataModels
@using jakubholovsky.com.Enum
@{
 ViewBag.Title = "Jakub Holovsky - Blog - Post - " + Model.Post.title;
}
<h2>
 @Model.Post.title</h2>
<div>
 SOME CODE
</div>
<script type="text/javascript">
 $(document).ready(function () {
 alert("hello");
 $('#addACommentDiv').click(function () {
 alert("hello");
 if ($('#commentFieldset').is(':hidden')) {
 $("#commentFieldset").slideDown("slow");
 $('#addACommentDivButton').html('Close commenting');
 }
 else {
 $("#commentFieldset").slideUp("slow");
 $('#addACommentDivButton').html('Comment on this article');
 }
 });
 });
</script>

I have tried to reference jQuery in my view but no luck. I also tried to use basic javascript(just alert window) to check if that works and it does. I also checked if jQuery is loaded in browser (it is).

The alert("hello"); line is there just to see if it passes the ready function.

This is how I have it referenced in my layout page.

<head>
 <meta charset="utf-8" />
 <title>@ViewBag.Title</title>
 <link href="@Url.Content("/Content/Site.css")" rel="stylesheet" type="text/css" />
 <script src="@Url.Content("/Scripts/jquery-1.7.2.js")" type="text/javascript"></script>
 <script src="@Url.Content("/Scripts/jquery-1.7.2-vsdoc.js")" type="text/javascript"></script>
 <script src="@Url.Content("/Scripts/modernizr-2.5.3.js")" type="text/javascript"></script>
 <script src="@Url.Content("/Scripts/google-analytics.js")" type="text/javascript"></script>
</head>

Thank you for helping me out.

tereško
58.5k26 gold badges100 silver badges151 bronze badges
asked Aug 7, 2012 at 16:07
2
  • Whats the error you're getting? Commented Aug 7, 2012 at 16:09
  • And if you aren't getting an error, try running the page through some kind of internet browser dev tools and see what error message you get Commented Aug 7, 2012 at 16:10

2 Answers 2

1

I suggest using the bundling feature so that you can use a wild card to select your scripts. this will help with future version upgrades of your scripts.

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
 "~/Scripts/jquery-1.*"));

and then render it in your layout or html somewhere

 @Scripts.Render("~/bundles/jquery")
answered Aug 8, 2012 at 1:27
Sign up to request clarification or add additional context in comments.

Comments

0

Before the / of each link, try adding the "~" tilde symbol. I ran into this issue as well. Also, try dragging the physical js file (from the solution explorer) onto this layout page. It will confirm if you got the folder structure/path correct.

The last thing I've had to do is use something like "../" How many times you use it depends on how many parent folders it must go up.

Hope that helps.

answered Aug 7, 2012 at 16:12

2 Comments

The ../ part of your reply is wrong. Url.Content is used to make all path relative to the web app root (which is represented by ~). But yes, adding ~ should do it. You should open your Javascript console next time. That kind of mistake will jump in your face looking at the console. (F12 on Chrome and IE without any plugin. Install Firebug for Firefox and F12 when installed.)
Hello, so referencing my scrips like this: <script src="../../Scripts/jquery-1.7.2-vsdoc.js" type="text/javascript"></script> <script src="../../Scripts/jquery-1.7.2.js" type="text/javascript"></script> <script src="../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script> helped! I am so glad I have solved it with your help. I just simply dragged and dropped the file. It is so damn weird that it has to be done like that.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.