This path doesn't mean anything to the browser:
<script src="~/Scripts/main.js"></script>
Unless the current folder literally has a sub-folder called ~
, then that won't find anything. It needs to be the actual path to the file. For example:
<script src="/Scripts/main.js"></script>
or:
<script src="../Scripts/main.js"></script>
or whatever the path to that JavaScript file is from the currently loaded URL.
The JavaScript code itself doesn't do anything differently whether the script
tag contains the code as content or references it from another source. It behaves the same either way.
Based on the comments below, if you're just asking how to create a function in JavaScript then that's simple. Something like this:
function addClasses() {
$("html").addClass("new1");
$("body").addClass("new2");
}
Then you can call that function any time you like:
addClasses();