asp.net unable to locate a resource file - javascript

Im attempting to utilize some custom script and css files within an asp page. In Visual Studio 2010 I am not getting any warnings or errors as to the status of these files, but when I attempt to run the page, and I open the javascript console I get the error:
Failed to load resource: the server responded with a status of 404 (Not Found)
Here's how I am attempting to load the files in my ascx file:
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript" src="scripts/jquery.scripts.js"></script>
<script type="text/javascript" src="scripts/jquery.alerts.js"></script>
<link href="styles/jquery.alerts.css" rel="stylesheet"/>
Anyone know whats going on here, why the browser can't locate the files but visual studio can?

This happens when the location of the .aspx file is different to the location of the user control (ascx).
When the user control is rendered in the browser it will actually have the location of the .aspx and tries to make the reference from that point. Whereas in VS, it will try to make the reference to the file from the .ascx location.
Therefore, try referencing the files as if you were making the references from the .aspx file location. It shouldn't give you any error when rendering that page.

I've upvoted aleafonso's answer since he's correct. Now, to solve this issue, you can use the ResolveClientUrl method.
Something like this:
<script type="text/javascript" src='<%=ResolveClientUrl("scripts/jquery.js"%>'></script>

Related

Can't load javascript file in localhost using xampp

I am new at web development and I want to upload a main.js scrip in my PHP file but it gives me this error
I used a script tag to upload the js file like this:-
<script type="text/javascript" src="../assets/js/main.js"></script>
This could be due to many things:
Check ad/script blocker
Check if the browser can load it from the view page source option
Try pasting it before all scripts call it could be due to some functions in previous scripts, I faced this issue before.
This might help.

How to properly call internal js.file after building in Javascript

I have a problem with initializing internal file.
Specifically https://www.detectadblock.com/
I can't initialize this ads.js file on my project.
var e=document.createElement('div');
e.id='punVTqCWOHsB';
e.style.display='none';
document.body.appendChild(e);
My web browser always failed with loaded script. Any suggestions how can I proceed?
Loading failed for the with source “127.0.0.1:8888/AdBlock/src/com/example/myproject/public/ads‌​.js”." github.com/marosmamrak/AdBlock-1.git
You are giving the full path to the source files (in src/.../public), but that file will be copied with the rest of the GWT compiled sources, so it should be inside the adblock directory to match your .nocache.js file:
<script type="text/javascript" language="javascript" src="adblock/adblock.nocache.js"></script>
<script type="text/javascript" language="javascript" src="adblock/ads.js"></script>
Also please note that this does nothing at all, since the browser can't execute Java files directly:
<script src="/AdBlock/src/com/example/myproject/client/Flipper.java"></script>

JS File Not Loading When URL is Directory

I wrote a simple 404 page for my website that tells you a joke every time it is loaded. I am accomplishing this with the use of the StackExhange API and am drawing questions off of this thread.
Everything works as intended when I purposely trigger the 404 page with a garbage file url. For example when I go to www.domain.com/garbage, the 404 page loads as expected. My 404.shtml has a script tag that loads a 404.js script that I wrote and everything is great.
However, when I attempt to get a 404 page by routing to a garbage directory as apposed to a garbage file, the script cannot be found. For example, www.domain.com/garbage/ does NOT load the 404.js script despite the correct 404.shtml page being loaded.
Here is the head section of my HTML file.
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"
type="text/javascript"></script>
<script src="404.js" type="text/javascript"></script>
</head>
Note: The external jQuery script is loaded both times, but 404.js is only loaded for the first request example.
Here is the console for both requests, the first one is the request for a garbage file and the second is the request for a garbage directory.
First URL: www.domain.com/garbage
// a long JSON response from the StackExhange API that indicates that the script loaded
Second URL: www.domain.com/garbage/
[Error] Failed to load resource: the server responded with a status of 404 (Not Found) (404.js, line 0)
Question What is causing the 404.js script to not be loaded when I attempt to access an invalid directory within my site when the same script loads when I attempt to access an invalid file?
What I Have Tried
A plethora of Google searches.
Triple check that all script tags are pointing to the correct files
Thank you for your time. If you wish to do some more digging, you can see the actual problem in action at briantracy.xyz/garbage and briantracy.xyz/garbage/.
Big Thanks to #rfornal for suggesting making a relative file path to the script. Channging the following line in the head section
<script src="404.js" type="text/javascript"></script>
^^^^^^
by prepending the root directory (where 404.js is located) to the file path does the trick.
<script src="/404.js" type="text/javascript"></script>
^^^^^^^

Javascript not found in my ASP.Net Application

I am having a problem in the location of my javascript, the location is right, but when I run it in my visual web developer 2010 express, the location can not be found, I don't know the reason why...
here is the location of my javascript:
<script src="Style/javascript/jquery-1.7.1.js" type="text/javascript"></script>
here is the error:
**Server Error in '/Maitenance' Application.**
**The resource cannot be found.**
**Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.**
**Requested URL: /Maitenance/Maintenance/Style/javascript/jquery-1.7.1.js**
Use this... It will work
<script src="<%=Page.ResolveUrl("~")%>Style/javascript/jquery-1.7.1.js" type="text/javascript"</script>
I guess you are using a master page and your .aspx page is put in another directory. The file path you included in master page is relative to the .aspx file. It works OK when your page in the same directory with the master page.
You can include your js file using ResolveUrl:
<script src="<%=ResolveUrl("~/js/jquery.js")%>" type="text/javascript"></script>
or you can include your script in the code behind of master page:
ClientScript.RegisterClientScriptInclude("jquery", ResolveClientUrl("~/js/jquery.js"));

Can't access Javascript file

I'm using webform asp.net and C#. I linked the master page with javascript file like this
<script type="text/javascript" src="javascript/main.js"></script>
but when I'm going to inner pages inside folders.. I can't access the JavaScript file.. (404 error).I tried to solve the problem by using ResolveClientUrl but it's not work!
What's the problem?
but when I'm going to inner pages inside folders.. I can't access the JavaScript file
Since the page you're accessing is at a different level than your master page, you want to make the script path relative to the root of your application
<script type="text/javascript" src="/javascript/main.js"></script>
EDIT if that's not working, then I would keep the script tag as it is above, run it in Chrome with the developer tools / network tab open, and look at the exact address that shows up for the failed request. Then look closely at your application and see what's different / wrong.
You can do:
<script type="text/javascript" src="<%=Page.ResolveClientUrl("~/javascript/main.js") %>" />
Allows you to use ASP.NETs ~. If this doesn't work then the file doesn't exist in that directory.

Categories