1

So i have three files.

html has:

<script type="text/javascript" src="~/Scripts/custom-notebook.js"></script>

custom-notebook.js line gives a 404 not found:

$.getScript('Scripts/custom-notebook-services.js'); //this gives 404

1. custom-notebook-services.js exists. It used to work but I changed html pathing to include the ~/ because I have a virtual application (yes MVC C#)..

2. Now nothing I do works (yes also searched around).

  • I have tried $.getScript('../Scripts/custom-notebook-services.js');

  • and $.getScript('../../Scripts/custom-notebook-services.js');

  • and $.getScript('/Scripts/custom-notebook-services.js');

No luck:

Always gives me a 404 with path

domain.com/Controller/Scripts/custom-notebook-services.js

...instead of proper path:

domain.com/Scripts/custom-notebook-services.js

kingPuppy
  • 2,937
  • 1
  • 18
  • 18
  • Try using the `~` in other links too. See how that works out – Dhrumil Apr 28 '15 at 17:01
  • wierd is showing 404: `domain.com/Controller/~/Scripts/custom-notebook-script.js` . – kingPuppy Apr 28 '15 at 17:09
  • 1
    Try it as `` – Jasen Apr 28 '15 at 17:10
  • right, if I absolutely have to....I will use that, but I am calling a javascript file from a javascript file. So razor syntax won't do any good there. – kingPuppy Apr 28 '15 at 17:13
  • 1
    Then the `~` won't work there – Jasen Apr 28 '15 at 17:14
  • If you have a DOM element you can insert the url there `data-url="~/Scripts/script.js"` and pull it into your javascript. Otherwise, drop the leading directory separator. – Jasen Apr 28 '15 at 17:16
  • Jasen, thanks for the comments, I think that helped. I am probably going to call the script on the html page (wrapping it in a event handler so the time works the way I needed it). Basically what you are suggesting above (calling the script from html, even though it is not how I really wanted to do it). – kingPuppy Apr 28 '15 at 17:42
  • 1
    ahh. excuse me. I see what you are saying. leave the path in the html and grab it. problem is not matter what I do it includes the `Controller` in the url...... unless I call the entire domain, which would work. – kingPuppy Apr 28 '15 at 17:44
  • What does `@Url.Content("~/Scripts")` print? Is `Controller` the name of your app? – Jasen Apr 28 '15 at 17:50

2 Answers2

4

One way to get the path from the Razor helper into your script is through a data attribute.

<body data-script-dir="@Url.Content("~/Scripts")">

Now extract that in your javascript but you'll need to delay your script until DOM loaded.

$(function() {
    var scriptDir = $("body").attr("data-script-dir");   //  "/Scripts"
});

then you can append the rest of your path to that string.

Jasen
  • 14,030
  • 3
  • 51
  • 68
  • I am upvoting Jasen's, this may be close.... you would probably need to concatenate a full domain url into the `data-script-dir` using the method above and building the path using http://stackoverflow.com/a/21226409/3023642 . For my purpose, I used a work around. This is a very strange problem! jQuery seems not to play well with MVC virtual directories. – kingPuppy Apr 28 '15 at 18:07
2

You have the virtual directory well configured on IIS? On the same directory you have the page that contains this <script type="text/javascript" src="/Scripts/custom-notebook.js"></script>, you should have a virtual directory pointing to "Scripts" on that same directory of the page.

Filipe Paulo
  • 134
  • 6
  • Right, so your saying `Script` dir should be in the virtual directory `root` yes? That is what I am doing. – kingPuppy Apr 28 '15 at 17:25