You need to sign in to do that
Don't have an account?
asadim
Executing JavaScript inside Apex (!!)
Hi,
We're trying to bring the functionality of one of our s-controls into Apex. However this particular s-control uses the JavaScript eval() method to run some JS code that is passed to it. The JS code is retrieved from a custom object. The question is, how can we still execute these JS scripts if we switch to Apex?
Thanks!
Running JavaScript in VisualForce/Apex is no problem at all.
You can achieve the same functionality that you got using S-Controls.
Simply enclose your JavaScript code within the <script>...</script> tags and you should be fine.
Hey
If you post some code we could probably guide you a bit better.
Wes
@ Aslam: Can you give me an example of how I would include <script> tags within Apex? Is there a built-in JS compiler in Apex?
@ Wes: Assume I have a method with this signature:
String executeJS(String script)
"script" can be any JavaScript code. I want this method to execute the script passed to it and return the result as a string (imagine all scripts passed to this method return some valid string value after execution).
Thanks!
I am not sure why you want to pass a Javascript to a Apex method?? Dont know if thats possible even.
Javascript is a client side script that can validate some user input.
A simple VF illustration of this would be like follows...
<apex:page controller="SomeController"> <!-- where SomeCntroller is a apex class that will proecess this page -->
<apex:form>
<apex:inputText id="input" value="{!someValue}"/>
<apex:commandButton value="{!save}" value="Save" onclick="return validate();">
</apex:form>
<script>
function validate(){
if(document.getElementById("{!$Component.input}").value==''){
alert('Error:field blank");
return false;
}
}
</script>
</apex:page>
This code might give you some idea, though it might not be what you want.
Or if u want to invoke an apex class method based on an action like onclick then u can use apex:actionFunction.
Hope this helps...
I would agree with this answer, although you'd have to shift the code around a bit,
<apexage controller="SomeController"> <!-- where SomeCntroller is a apex class that will proecess this page -->
<apex:form>
<apex:inputText id="input" value="{!someValue}"/>
<script> var v = '{!$Component.input}' </script>
<apex:commandButton value="{!save}" value="Save" onclick="return validate();">
</apex:form>
<script>
function validate(){
if(document.getElementById(v).value==''){
alert('Error:field blank')
return false;
}
}
</script>
</apexage>