function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
asadimasadim 

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!

Aslam_Kamal_86Aslam_Kamal_86

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.

 

 

WesNolte__cWesNolte__c

Hey

 

If you post some code we could probably guide you a bit better.

 

Wes 

asadimasadim

@ 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!

Message Edited by asadim on 11-18-2009 10:42 AM
Aslam_Kamal_86Aslam_Kamal_86

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...

WesNolte__cWesNolte__c

I would agree with this answer, although you'd have to shift the code around a bit,

 

 

<apex:smileytongue:age 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>

</apex:smileytongue:age>