0

I am sure there is a very simple answer to this question, I think am just having trouble. I have animation.js file. It also has a include folder with dependency_1.js and dependency_2.js. In my animation.js file I do load dependency_1.js and dependency_2.js relative to my animation.js file.

In my _Layout.cshtml file I do have:

@RenderSection("JavaScript", required: false)

And I did place within my view SomeView.cshtml

@section JavaScript
{
  <script type="text/javascript" src="@Url.Content("~/Scripts/animation.js")"</script> }

But when my animate.js file calls the dependency_1.js file I get a 404 Not Found - http://localhost:14611/includes/dependency_1.js"

Im guessing it has to do with the url routing to the Scripts folder in my MVC project?

1 Answer 1

1

Relative paths in Javascript files are relative to the HTML page, not the source Javascript.
You need to use an absolute path.

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

2 Comments

I can see how that works, MVC3 lets you place @Url.Content("~/Content/style.css") to get a path no matter what view you are using. I have added js to several views with no problem. Yet, I cant place this attribute within my js file. to use the dependency_1.js. Is there anything I can put (maybe my web.config) so that every view has access to these particular js files?
The simplest answer would be to include them explicitly in <script> tags. Other than that, you could use an inline <script> to declare var baseUrl = "@Url.Content("~/")";

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.