Reputation: 277
Anyone knows how to include javascript file as project's resource for aspx.cs files under different folders to use?thanks very much
Upvotes: 1
Views: 8352
Reputation: 66071
Suppose your project's directory looks like:
/App_Code
/App_Data
/App_Themes
/Master_Pages
...
web.config
Add a folder called "scripts" so you have:
/App_Code
/App_Data
/App_Themes
/Master_Pages
/scripts
...
web.config
Put your javascript file in this scripts folder.
Put the script tag in any of your aspx pages.
<script src="/ProjectRoot/scripts/your-file.js" type="text/javascript"></script>
The key to remember is the path you specify in the src attribute. This is relative to the root of your domain. If you are developing on your machine and you test your project at: http://localhost:2430/SomeProject/
Then your script tag needs to look like:
<script src="/SomeProject/scripts/your-file.js" type="text/javascript"></script>
If your project is deployed on a server at the root: http://www.example.com/
Then your script tag needs to look like:
<script src="/scripts/your-file.js" type="text/javascript"></script>
If your project is deployed on a server at the root: http://www.example.com/SomeApp
Then your script tag needs to look like:
<script src="/SomeApp/scripts/your-file.js" type="text/javascript"></script>
FYI, because the script tag is not a server-side tag you can't use the "~/" syntax. And because you can't use the "~/" syntax, you (usually) can't use a relative path for the src attribute but instead have to specify an absolute path from the root of your domain.
Upvotes: 1
Reputation: 187110
Put the javascript file in a folder in the web application project.
In pages where you want to refer the javascript file you can add this in your aspx file.
<script src="path_to_yourfile" type="text/javascript"></script>
Upvotes: 0