I don't know if this is realizable, because i don't have strong knowledge in JavaScript.
I want to use Java variable in Javascript and XHTML code, i have a file path in java variable: applicationDadesVersioBB.rutaFitxerLog which i display as follows in XHTML page in a table as follows:
<tr>
<th scope="row">#{literalsCore['Entorn.FitxerLog']}</th>
<td>
#{applicationDadesVersioBB.rutaFitxerLog}
</td>
</tr>
until now everything is fine, but what I want to do is once the path is clicked, the file should be downloaded, which i am not able to do with: onClick="descarga([[${applicationDadesVersioBB.rutaFitxerLog}]])
the JavaScript descarga function:
<script type="text/javascript">
function descarga(file)
{
window.location=file;
}
</script>
I think the problem I have is the conversion of java variable values to javascript values.
I tried to do it changing the onClick="descarga([[${applicationDadesVersioBB.rutaFitxerLog}]])" to onClick="descarga("#{applicationDadesVersioBB.rutaFitxerLog}")"
but no way, any help?
I'm not sure about what you're trying to do but I remeber a useful "hack" to do that in PHP for example... You have to create a hidden input in your HTML file and set its value to applicationDadesVersioBB.rutaFitxerLog. Then in your click handler you just get the input element thanks to its id for example and you read the value.
Hope it helps
you need quotes around a string
onClick="descarga('[[${applicationDadesVersioBB.rutaFitxerLog}]]')"
Related
Its as simple as it gets, i want to change the text within <td> to be whatever the user enters in the prompt, i have tried many solutions. Such as putting the script element below body. Here is the java script code however it doesnt work although i assume everything is as it should.
var yourname = prompt("Enter your name");
document.getElementById("name").innerHTML = yourname;
Here is the HTML
<html><head>Cant figure this out</head>
<body>
<table>
<tr>
<td>Name</td>
</tr>
<tr>
<td id = "Name"></td>
</tr>
</table>
</body>
</html>```
I see a couple of problems with the code:
The html doesn't have a script-tag to use the javascript. Without that it won't load. To avoid these kind of problems please make the smallest code that have the problem and use copy-paste to get the exact code in questions.
Name != name. The case of the id differs between the html and the javascript.
You have additional characters last in the html: ```
I'm not a frontend developer and don't know if this will solve your current problem. But those issues should be handled either way.
in your HTML you gave the an id of "Name", yet in the javascript script you are searching for an element with id "name". The id is case sensitive, so you would have to search for "Name", not "name".
So I have a HTML file with an embedded script. A Java application sends a value to this HTML file. Now I wonder how to pass this value from the HTML down to the script. Is this even possible?
Here is the simplified HTML file with my approach:
<html>
<body>
<div id="test">
[VALUE_FROM_BACKEND] // prints "let valueFromBackend = 1234"
</div>
<script>
console.log(document.getElementById('test').value);
// should return: let valueFromBackend = 1234;
// actually returns: undefined
</script>
</body>
</html>
Unfortunately, I can't pass the value from the Java application directly to the script. I got the above approach from here, but this doesn't work.
Other solutions only focus on getting values from remote HTML pages, declaring the HTML files's source in the script tag. But since it is an embedded script here, this also seems not to work.
Does anyone know how to deal with the situation? Help will be much appreciated.
Only HTML input elements have a value in javascript. A div cannot have a value, which is why your code returns undefined.
To access the text inside a regular HTML element, such as a div, use element.innerText instead.
Here is a working code snippet you can try out:
console.log(document.getElementById('test').innerText);
<div id="test">
let valueFromBackend = 1234
</div>
As you want to get value of a div element, so the syntax is:
document.getElementById('test').innerHTML
Remember that getElementById().value works for input and use getElementById().innerHTML for elements like div
I have created a wysiwyg text editor using an iframe and am trying to save the contents of this.
The HTML for my iframe is:
<iframe name="richTextField" id="wysiwyg" src="page?page_id=3"></iframe>
I am then using a hidden input to submit this to the database:
<input type="hidden" id="text_content" name="text_content" value="">
I am trying to get the contents of this into the value of the input field using JS like this:
$text_content = #wysiwyg.document.getElementsByTagName('body')[0].textContent;
$("#text_content").attr("value", $text_content);
If I set $text_content to just a random string it will work but it won't get the contents of the iframe.
I have tried $("#wysiwyg document body").textContent, #wysiwyg.document.getElementsByTagName('body')[0].textContent and richTextField.document.getElementsByTagName('body')[0].textContent.
richTextField.document.getElementsByTagName('body')[0].textContent is actually what I used in the JS for toggling to the source but it will not work when trying to set it as a variable.
Should I be trying to do something like php serialize() to get this as a variable I can work with? Or are the terms I've tried as my selectors just wrong? Any help with this will be greatly appreciated.
For future reference, this is what I used:
richTextField.document.getElementsByTagName('body')[0].innerHTML;
I am trying to find a way to have the document.getElementById("1").innerHTML to get triggered based on a condition coming from jsp code.
For example I have the following jsp code on the jsp page,
<table border="1">
<c:forEach items="${elements}" var="element">
<tr>
<td>${element.elementNumber}<br /> ${element.isReserved}
</td>
</tr>
</c:forEach>
</table>
Based on "element.isReserved", I need to change an element (supposedly using .innerHTML)in html. The element in html looks like the following. I have several of these elements.
<div class="col-xs-2">
<div class="o-btn">
<a id="1" onClick="myFunction(this);">1</a>
</div>
<div class="clearfix"></div>
</div>
As shown above, it is displayed as a button with value "1". I want to change the value to "X" based on a condition. I can do this directly with js,
<script>
function myFunction(elmnt) {}
function isReserved() {
return 'X';
}
document.getElementById("1").innerHTML = isReserved();
</script>
But I need to make it dependent on the output from spring or the jsp code shown above. My desired output is that the "1" displayed on the button will change to a "X" when the element.isReserved() (inside the jsp code) evaluates to true. Since I have several of the button elements, I need to check for every button if element.isReserved() evaluates to true.
I was able to make it work. I found this article very helpful.
After understanding (and accepting reality) how JSP works with html, I had to slightly restructure my program.
From spring mvc, I added my variables to the model,
model.addAttribute("element1", 1);
On the jsp page I checked the variables with javascript,
if("${element1}" == 1) {
document.getElementById("1").innerHTML = 'X';
document.getElementById("1").style.color = 'red';
}
Probably there are better solutions, but for a newbie like me, it works great!
The article I mentioned above is indeed a good and important read for new comers.
I have a coldfusion page and I am very newbie in coldfusion. What I need to do is to insert the alert in between to see what is the time. In php I could close the php tags and enter the javascript tag and alert out the value. How would I do that in coldfusion? I have this
<cfset right_now=Now()>
<cfscript>
alert(#right_now#);
</cfscript>
But its not working.
thanks
<cfscript> is a Coldfusion tag for using the Coldfusion scripting language (aka CFScript). If you want to use Javascript, open a <script> tag like you would normally in HTML. You'll probably want to make sure it's inside a <cfoutput> tag if you want to use Coldfusion values within your javascript.
<cfset right_now = Now()>
<cfoutput>
<script type="text/javascript">
alert('#right_now#'); // don't forget you need to put quotes around strings in JS
</script>
</cfoutput>
You don't need to even use cfscript for this specific need. You could, for instance, do this:
<script type="text/javascript">
var currtime = new Date();
alert(currtime);
</script>
... Also a point to remember, you can't directly output HTML from within a <cfscript> tag. You can however get around this by calling a function from within a <cfscript> tag that can output the data for you.
Always remember the coldfusion begins and ends before anything else is executed: html, javaScript, sql, etc., so the javascript is getting an already formed code, which is CF instead of being hard coded.