what is rootpath in javascript src attribute? - javascript

I'm developing a .net web application. I want to link to a js file in one of my user control. I tried ./, I also tried ~, neither works. what is rootpath in src attribute?

<script src="#Url.Content("~/Scripts/filename.js")"></script>
Depends on what .net version you're working in. This is MVC 3 Using the Razor Syntax

Just drag and drop that js file in your control it will automatically link your js file to the control without any error.

Related

How to add reference to a script file in sublime text to code in angular js?

I am trying to code the below sample program in Angular js using Sublime Text. I have already included the angular package in Preferences. The code doesnn't work when I give the src for angular as src="angular.min.js", where as it works I give the src as src="https://code.angularjs.org/1.4.0/angular.js". How do I link the src correctly on my computer?
Just download the reference that you use and save it as angular.min.js on the same folder where your app is.
Also no need to have multiple ng-app mentioned in the div.
If you are having a separate controller defined, it is necessary to define the ng-app with the module name and mention the controller in the view.
Try running Sublime Text as Administrator.

No intellisense in javascript (*.js) file when using T4MvcJs

I have started using T4MvcJs in my project recently. The problem I am facing is that, I am unable to use Intellisense feature in my javascript file (.js) when using T4MvcJs (generated strongly-typed) URLs, and I have to type precisely all strongly typed URLs without intellisense. Note that, javascript (.js) file is separately maintained from its razor view file (*.cshtml). This is making the usage of T4MvcJs very difficult.
Please guide, whether intellisense feature is available in T4MvcJs or not? If availble, kindly guide how to enable/use it?
For example: The url in my javascript file w/o using T4MvcJs is declared as
var _url = "/Home/Index"
Kindly guide, how can I write the above url using T4MvcJs in my javascript (*.js) file. Also explain, how to achieve it using intellisense.
Application Plateform: C# + MVC4 , VS 2013
If you have a script folder in your application put the jQuery file in that folder and create another script in that folder name it _references.js
in the references.js
add this code
/// <reference path="The name of jQuery file you want to reference if its in the same folder" />
if you cannot keep the jQuery file in your folder then give the appropriate path
of the jQuery file that is located and the intellisense of the jQuery file will be available to you.
This works for me whenever I add a new nugget package I add the reference path in the references jscript file give it a try and let us know

How add Javascript in GVnix/Spring roo project

I'm developing a web application and I want add a js file in my project to use it. I tried to add in this way:
<spring:url value="/resources/scripts/gestion.js" var="gestion"/>
<script src="${gestion}" type="text/javascript"></script>
The js loads correctly, but the problem is that the page is not showing anything, if I put this code in the end of the jsp file, before of tag <div> the jquery and another js file of the project not work although I can see the content of the jsp.
Somebody can help me with this?
You should include it into src/main/webapp/WEB-INF/tags/util/load-scripts.tagx file.
This tag is used by src/main/webapp/WEB-INF/layouts/default.jspx which is used by Apache Tiles configuration file src/main/webapp/WEB-INF/layouts/layouts.xml to generate the final html output.
Good luck!

Web page URL link with JS onclick relative path?

I have an old web app developed in ASP.Net with the old type code behind style. I am eventually going to re-do the complete web app in ASP.Net MVC 3 - just starting to complete the first sections and integrate them. I am having some problems with a menu that appears along the top. So there is a home button it looks like the below in code:
onclick='document.location.href=\"Home.aspx\"'>
However I am getting the url in the new mvc part of web were I have the menu and i try to hit the home icon I get a page not found:
/MyWeb.Web.App/mvc/Controller/Home.aspx
The actual home aspx page exists in the location \MyWeb.Web.App\mvc\Home.aspx
I am currently stumped as to what I can edit in the JS href to get it to point to this location so the icons will work in the existing implementation and the new MVC one.
Thanks.
Use urls helpers. For example if you are using Razor and this code is inside an MVC view:
onclick='document.location.href="#Url.Content("~/mvc/home.aspx")"'>
or if you are using the WebForms view engine:
onclick='document.location.href="<%= Url.Content("~/mvc/home.aspx") %>"'>
UPDATE:
If this is in a separate javascript file then you could declare a global variable inside your Razor view that will point to the base url:
<script type="text/javascript">
var baseUrl = '#Url.Content("~/mvc")';
</script>
and then inside your javascript file:
onclick='document.location.href=\"" + baseUrl + "/home.aspx\"'
There are of course much better ways to achieve that but since you haven't provided enough details about your scenario it's difficult to suggest what might be best. For example you could use HTML5 data-* attributes in your DOM or some other elements to generate properly this url and access it in your javascript file.

How to get visual studio to recognize a cshtml file as javascript

I am writing a css/js page that has some dynamic parts in it.
To do this i am using a cshtml file containing css/js - i am using mvc.net and returning the css from a controller action.
The trouble is visual studio recognizes this page as html and not as javascript/css so it does not give me javascript/css coloring and IntelliSense.
My questions:
is there a better/easier way of creating dynamic css/js in .net
How can i get visual studio to recognize a cshtml page as javascript.
I know this is an old post...but...
What I did was just put the script tags around my javascript files in their own .cshtml file.
I created a separate controller (JavascriptController.cs), and I created a filter on that controller that removes the script tags. I set the filter in the OnActionExecuting method. by just doing
this.Response.Filter = new ScriptFilter(Response.Filter, Response.ContentEncoding);
So you get the syntax, you get razor without having to use RazorJS, and you can request the js files like regular routes in an MVC application. You just have to keep the script tags on the partial view while editing.
So you can call
/Javascript/{Action}
and you'll get your javascript file with your razor in it, and the filter will remove the script tags so you can include it like a normal script.
<script src="http://{host}/Javascript/{action}"></script>
What is wrong with putting <script> and <style> tags in a page and put your dynamic js/css there , in the end if it is dynamic there is no way for caching it so this approach will be fine.
you can write something like below:
<script>
function myFunction_#MyFunc(params)(obj) { return obj.field + #MyOtherFunc(params); }
<script>
and razor engine will evaluate #MyFunc(params) and #MyOtherFunc(params) before sending it to browser
The best solution is to follow unobtrusive javascript and unobtrusive styling and put your javascript into a .js file, put your css into a .css file and reference them in the markup in your cshtml file with a <script> and <link> tag.
e.g.
<script src="Scripts/scriptName.js" type="text/javascript"></script>
<link rel="stylesheet" href="Content/styleSheetname.css" type="text/css" />
This is good practice as it keeps your content(markup)/styling/behaviour separate.
For JavaScript you could try the RazorJS nuget package. But we've run into some inconsistencies while using it.
Still trying to find a better way to do this using Controller/Views and still be able to use intellisense and get decent coloring.

Categories