1

I will made a sample code just to describe my problem, here is the code that I originally use on the view:

@model MyProject.Web.UI.ViewModels.MyModel
@{
    ViewBag.Title = "Test";
}

@section scripts{
    <script>
        $(document).ready(function () {
            alert('Test');
        });

        ... more code here (removed for brevity)
    </script>
}

This one is working well, but as a good practice, I need to move the javascript/jquery code to an external file so the above code turned into this:

@model MyProject.Web.UI.ViewModels.MyModel
@{
    ViewBag.Title = "Test";
}

@section scripts{
    <script src="/Js/external-js-file.js">
    </script>
}

and in my external javascript/jquery file:

$(document).ready(function () {
    alert('Test');
});

... more code here (removed for brevity)

and to my surprise, the alert wasn't even firing, all of the jquery and javascript code is suddenly not working, I've been googling a lot and I can't find any solution to this, am I missing something? Thanks in advance guys.

5
  • 1
    Check your console for errors - is the file being loaded? Commented Jul 30, 2014 at 3:40
  • yup, definitely, I've viewed the page source and the file is actually loaded. Commented Jul 30, 2014 at 3:41
  • Press F12 in your browser and refresh.. see if you are getting 404 or any other error. Commented Jul 30, 2014 at 3:46
  • just to confirm. Have you included jQuery. If included it must be before this script Commented Jul 30, 2014 at 3:50
  • Yup iJay, I included it before the external javascript. Rosdi, no 404 error man. Commented Jul 30, 2014 at 3:57

1 Answer 1

1

Hi guys nevermind I've found the culprit! I've copy & paste the javascript code and I found out that there is a server side comment included on the script, stupid me.

@* razor comment *@ 

I appreciate the prompt reply guys, next time I'll be more careful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.