How to get visual studio to recognize a cshtml file as javascript - 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.

Related

Angular Dart external JS

I am building an Angular-Dart site based on a commercial Bootstrap template.
The correct rendering should be like this:
I used IntelliJ to scaffold a Dart/Angular app and started to modify from there.
I have put related files (CSS/JS/images) of that template into web/css, web/js, respectively.
HTML used is verbatim copied from the template but I have taken out the CSS, JS reference from btqun_component.html and moved into index.html.
The output is like this:
Obviously, the CSS is working, and the header/footer are showing correctly. But the masonry effect is not showing, so I doubt that is related to JS reference.
Can anyone give any hints on this?
Do you have a documentation for the bootstrap template ? I guess you need to execute the javascript they provide to you so you need to add it to your index.html, and you probably need to import bootstrap and jquery too.
If you need to call a javascript function you can do it directly in the index.html inside a script tag or build a dart wrapper using package:js
EDIT: answer to call jQuery function from Dart

How to embed javascript file content into html page in Thymeleaf?

I need to embed javascript directly into html page generated by Thymeleaf
Something like:
<script th:include="../static/assets/generated/scripts.js"></script>
But this simple usage leads to SAXParseException...
Is there any easy way to switch off parsing of the th:included
content? Or any other way how to embed content of resource int the result page?
I don't think that is possible out of the box. You could probably write an extension that can do it. Or maybe there is an existing one, but I couldn't find one right now.
Does it have to to be a separate JavaScript file? Can't you put your JavaScript code into a fragment and include it like any other fragment?
NB: Including JavaScript into your HTML file like that is usually bad web design und may be a sign that you have bigger problems and you haven't structured your code well. Why do you think you need to do that? Why can't you refer to an external script file?
Thats not a Thymeleaf-Thing. It's classic html:
<script src="/assets/generated/scripts.js"></script>
In version 3.0, you can do it in this way
<script th:src="#{/webjars/jquery/dist/jquery.min.js}"></script>

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!

Extract javascript from JSP to JS file

I'm trying to refactor parts of our front-end at the moment, using Intellij. We have a lot of javascript that is within our JSPs. I'd like to be able to extract from the JSP into a .js fil and replace it with a script tag referencing that file. I know how to extract to an 'includes' file, but I'd like a way of extracting and replacing the code with a proper script tag.
Does anyone know of a plugin that can accomplish this quickly and easily? (Or a pre-existing function in Intellij that I'm unaware of)
Please vote for this issue (it's 5 years old and had zero votes).
just create one file with .js extension copy and paste your js codes with their function names. then include that file in your jsp project header section.
eg:
<script type="text/javascript" src="js/jquery-ui.min.js"></script>
thats all..

How to include my javascript file after Telerik includes their javascript

I would like to override Tekerik's javascript function. To do that I want to include a file that contains modified javascript after Telerik's js include.
I include my js files using ClientScriptManager.RegisterClientScriptBlock method. My files are included before Telerik's preventing me from overriding their function.
<script type="text/javascript" src="myScriptFile.js"></script>
<script src="/atlas/Telerik.Web.UI.WebResource.axd?....></script>
How can I include my javascript file after Telerik's?
One option that I found is to use RegisterStartupScript method that
"emits the script just before the closing tag of the Page object's <form runat= server> element."
which is after Telerik includes their files. This doesn't include a js file but at this point I can call a function that overrides Telerik's function.
Assuming you want to do this globally, had you considered editing the Telerik source file rather than overriding it?

Categories