I am trying to execute Java method in Javascript over object which I get from Java code using Freemarker.
I have java object "actual_id" of type "Settinga" and method "setActual_id" which just sets one attribute of the object. This is my call of the method in js:
var idd = ${actual_id}.setActual_id(variable);
Instead of execution in html code I see:
javaclassurl$Settinga#5beccfce.setActual_id(variable);
And there is error: myFunction is not defined
at HTMLButtonElement.onclick
Why it doesnt execute?
Following call is executed as expected but I need to include variable:
var idd = "${actual_id.setActual_id("aaa01dd3-abe4-4d50-a69e-62b04199b7c5")}";
I understand that Freemarker expressions are translated during page generation and when I use just string its easily interpreted and can be executed but during page generation my variable which I want to include in the call isnt known so need to make execution somehow else...
Because the way it works is this: FreeMarker is executed on the server, resolving all the ${...} to mere text, then the browser on the client side gets the resulting HTML and executes the JavaScript.
Related
Im using Hugo and Im trying to pass a markdown parameters from md file to JavaScript file.
This is the markdown file
This is my javascript file
Are you using your own theme, or something you have downloaded?
You can edit the relevant template to output the parameter as a Javascript variable. And after that, use a Javascript function to reference that variable by name. Example:
<script type="application/javascript">
var title = "{{.Title}}"
console.log(title)
</script>
In this case, you would probably want to add a check to ensure that parameter exists, or is properly set before trying to print it. Otherwise the JS may throw an error if the variable is null or undefined.
Hugo is a static website generator - so it outputs HTML - if you want a "variable" from markdown to Javascript - with traditional JS and Markdown (i.e. no products like Alpine or something beyond the scope of Hugo) you would output the HTML and have Javascript - getElementbyID for instance.
I have a string which contains html script tag which contains some javascript code. I receive that code from another server via websocket.
That string contains some javascript code which performs some changes to the DOM. so I want to evaluate that string. I know that The eval() function evaluates JavaScript code represented as a string. But when I evaluate that string I'm getting some escaping error.
Is there any way to append string with javascript code into the DOM and perform the evaluation of that piece of code at the time of their insertion into the DOM.
So consider I have a variable chain like bellow
let chain = "<script>console.log('test')</script>";
I want to append that string which is a representation of a html script tag into the DOM and I want that to be evaluate at the same time it's inserted into the DOM. After that I'll expect to have some log into the browser console.
I think this is what you are looking for.
Note: I had to change the closing tags in the string from </script> to <\/script> to make the code snippet work on SO:
let chain = "<script>console.log('Test')<\/script>";
let code = chain.replace('<script>','').replace('<\/script>','')
let script = document.createElement('script')
script.onload = eval(code)
document.body.appendChild(script);
If you're just wanting to perform eval on the string you're receiving, you need to strip the tags and eval the new string.
The following snippet should work fine for your basic requirements. Just remember that eval is not recommended.
let chain = "<script>console.log('test')<\/script>";
let code = chain.replaceAll(/<\/?[^<>]*>/g, "");
console.log(chain, code);
eval(code);
I have looked but I simply cannot find an answer to what I expect should be a very simple task.
I have navigated to a page that contains a known javascript variable.
eg: var foo='my variable text';
How can I extract that value ('my variable text') using iMacros browser? Is that even possible to do directly?
Or, would I have to execute javascript to place that variable's value into an input or tag, that iMacros can then extract from?
Many thanks in advance.
You can use the URL GOTO method to call a javascript function, which will load the value inside the input element. Please checkout the below example where I have replicated the same.
HTML:
<script>
var a = "naren";
</script>
<input type="text" id="naren"/>
IMacros:
VERSION BUILD=9030808 RECORDER=FX
TAB T=1
URL GOTO=javascript:{(function<SP>(){console.log(document.getElementById(a).value='works!')})()}
So I have made a mockup page with the html as above, then ran the below imacros.
Some points you need to know about calling the imacros script are:
I have wrapped the anonymous function as an iife, long story short, the function will get executed immediately.
If you visit the IMACROS URL wiki page There is a line which tells us.
The URL GOTO command by default outputs the result of the evaluated
Javascript expression on a new page in the current tab. If you want to
manipulate an element on the page and don't want the result to be
processed by the URL GOTO command, you need to wrap it in some other
call or expression that does not evaluate to any value.
For example, to change the value of the Name field on the demo Fill
Form page, you can wrap the call in console.log() as follows:
URL GOTO=http://demo.imacros.net/Automate/TestForm1 URL
GOTO=javascript:console.log(document.getElementById("name").value='Test');
So I have implement the above statement by wrapping the assignment done to the input element inside a console.log.
Please let me know if you have issues implementing this solution!
I have a JS function which I want to loop round a c# object passed from the controller. I then want to compare it to a HTML input value - which I have passed into the JS function as a string.
Function is called like
searchReleases($("[name=release]").val());
function searchReleases(givenName) {
var list;
var data = givenName;
#foreach (var a in #Model.Releases) {
if (#a.Name.Contains(givenName)) {
list+= a.Name + "=";
}
}
}
However I can't access the JS variable givenName within the IF statement.
I have spent a while trying to find an answer on google but have yet found a workable solution.
You should understand that C# is a server-side code and JS is executed on client-side.
So if you generate view in ASP.MVC you have access only to C# data. All information which you want to print on your page will be statically generated to HTML file.
Then you can pass this HTML page to browser and inside it, it would be executed JS code. But only based on statically generated data or JS variables.
You can't read JS from C#, but you can read C# from JS. So if it's a read-only operation, than convert your C# object to JSON and access it from the JS code.
Try this answer : Asp.net mvc passing a C# object to Javascript
I have javascript function sample('textValue') and have to call at server side on anchor click. I tried below code
string text="xyz";
anchor.Attributes.Add("onclick","javascript:sample('"+text+"');
but the value of the text is not assigning correctly. Encoded string gets added. The result in view source looks like
javascript:sample('xyz')
But i need javascript:sample('xyz')
What server/backend language do you use? PHP? Do you use any framework (Zend, CakePHP...)?
On the JS side do something like this:
Option 1
Test
Option 2
Test
<script type="text/javascript">
document.getElementById('clicky-clack-link').onClick = function() {
sample('test');
};
</script>
Note: Also check out jQuery if you haven't.
I wonder if you could just do this:
string text="xyz";
anchor.Attributes.Add("onclick", function(){ sample(text); } );
What does it do? Well, the onclick handler takes a function with no arguments, right? That is, what to do if somebody clicks the link. If you're coding this by hand in HTML, you can use the javascript:a_statement_goes_here to describe the code to run. I expect the browser will just create a function out of that. Since you're assigning this in JavaScript, you have to do that yourself (unless you write out to the document - that might work) and assign the function. But you don't have such a function yet - you have one sample that takes an argument - hence the anonymous function closing the text argument.
This is based on the assumption, that the above is actually client-side code. I'd be very surprised, if JS didn't allow you to assign a function to an attribute. In fact, I think the problem you are running into, is JavaScript trying to be very smart and make sure assigning a string, will stay a string - that is why your ' got encoded.
Have a go, tell me how it went. Ta!