Javascript not working with JSF - javascript

I'm working on a jsf page and I'm trying to program a button onclick event by javascript to be able to add rows dynamically in a jsf dataTable ; but the problem is that jsf is not loading javascript code anymore
I tried to use an external js file
<h:outputScript name="">
and also tried using the tag
Any suggestions please ? Thanks!

You can use either JSF <h:outputScript> or html <script> tag.
First, you need to create a folder under WebContent and name it as resources. Under resources folder, you can create folders where you want to put your resources such as CSS, javascript, images etc.
For JSF <h:outputScript>:
<h:outputScript library="script" name="/script.js"</h:outputScript>
Where the value of the library is the name of the folder which contains your javascript file.
Here's the code for HTML <script>:
<script src="#{request.contextPath}/resources/script/script.js"></script>

Related

How to add Django template tags in a different Js file

Hi I want to add the tag {{request.user.first_name}} in my js code. I am aware that if the js code is within the HTML document you can easily add any Django template tag in Django version 3.2.
But for now, my HTML is as such...
<script src="{% static 'dashboard/driver.js' %}"></script>
Which contains over 2000 lines, and I do not want to include it in my HTML file.
So my question is can I add the tag {{request.user.first_name}} and several others in a separate JS file, in my case driver.js.
Any help would be greatly appreciated.
Thanks!
You can't add any Django template code in js file directly.
There are 3 ways you can add the first_name in js file.
Write a django ajax views and call the api for first_name from your js file as soon as your page is loaded. or
render your first name in your html file hidden input fields and than grab the fields value it's name or id from your jsfile or
put a utill function on your html js <sccript> tags which are gonna call you driver.js file related method.

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!

Referencing js files from an html file in Quickbase

I am trying to build a flot chart using data from Quickbase. In order to accomplish this I have to be able to reference the flot.js files. In my html page I have scripts that call the js files located in Quickbase where the src is equal to the url link to the read-only version of the js file. However, the html page does not seem to be referencing the js pages. Does anyone have any suggestions? Thanks.
Use a script similar to this, but replace the part before .quickbase, the db, and the pagename value.
<script src="https://haversineconsulting.quickbase.com/db/bipznmwx8?a=dbpage&pagename=myscript.js"></script>
Or
<script src="bipznmwx8?a=dbpage&pagename=myscript.js"></script>
See https://quickbase-community.intuit.com/questions/811560-javascript-libraries

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?

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