It's actually a great time saver to do this. David, can you elaborate about what your build script does? I like this idea too, I am passing a lot of time struggling and looking for related files You could write this code in view's head to use the js file Scripts. Felipe Pincheira 5 5 silver badges 19 19 bronze badges. Cybercop Cybercop 8, 20 20 gold badges 66 66 silver badges bronze badges. Maroine Abdellah Maroine Abdellah 1 1 gold badge 4 4 silver badges 15 15 bronze badges.
You should Add rendersection to your Layout Page which your view using. RenderSection "scripts", required: false. Sign up or log in Sign up using Google. Sign up using Facebook.
Sign up using Email and Password. Otherwise, we can only guess what bug you have introduced. Open the browser's developer tools by pressing F Open the network view and verify that your external js file is rendered and not showing a Not found error.
If you see a error then your file reference is incorrect. Peter, you've been on these forums for a long time and IMHO long enough to do the basic troubleshooting as described above.
The content you requested has been removed. Ask a question. Quick access. Search related threads. Remove From My Forums. Active Oldest Votes. Always use ResolveUrl to call your script files like this Lets assume your script is in Script folder of your root path with a file Name as MyScriptFile. Suave Nti Suave Nti 3, 11 11 gold badges 53 53 silver badges 78 78 bronze badges.
I added this into my script src attribute and I am still receiving the same error. Any thoughts? Just checked why do you using initAll i. I just tried ResolveClientUrl and removing initAll function with every combination. Still a no joy! First I advice you to create another js file external with a test function and alert inside it and try checking if you can see the alert. Show 2 more comments. Based on your question : How to use an external javascript file in asp.
HatSoft HatSoft Rather than add a JavaScript file to our main project, we will do things a little differently. We are going to create a new project to hold all of our JavaScript files.
Start by adding a new class library project, called JavaScriptLibrary , to the solution:. When you added that project, a file called " Class1. We don't need that, so delete it. You will also add two JavaScript files to that folder. Call them " ShowMessage. By now, your solution structure should look something like the following:.
A couple of empty JavaScript files aren't going to do us any good, so let's put something in them. For starters, " ShowMessage. Notice that GreetUser is dependent upon ShowMessage. We will be handling that later. Rather than have to deploy our JavaScript files to our website, we'd like them to be embedded into the DLL. That way, if the DLL is deployed to the website, all JavaScript files are also automatically deployed.
Doing this is very simple; all we need to do is go to the properties for each JavaScript file and change the "build action" from "content" to "embedded resource":. While that will ensure the files get embedded into the DLL, you must still make them available to the web client.
Before you do that, the JavaScriptLibrary project needs to reference System. Web :. Once you have added that reference, edit " JavaScriptHelper. Now that you have embedded the JavaScript files and made them accessible via the client computer, you must actually reference them from the pages they are used on.
To facilitate this, we will fill in our JavaScriptHelper class:. The IncludeJavaScript function is what does all the magic. It ensures a script tag gets inserted for an embedded JavaScript file, which it does by calling RegisterClientScriptResource. Any page that includes the GreetUser function will also include the ShowMessage function. Now that we have that class available for our use, let's put it to use on the " Default.
0コメント