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
abinayaabinaya 

Pass Javascript value to apex class

I have a javascript variable which gets the current app name.

 

var myappname = document.getElementById("tsidLabel").innerText;

 

I want this 'myappname' in my apex class. I am not going to make any action on my visualforce page. On my page load i am doing some functionalities.

 

Please help me out. I am new to salesforce.

harsha__charsha__c

Call an action function from the javascript and pass the param through it.

 

An example is shown below.

 

<apex:actionFunction action="{!methodOne}" name="methodOneInJavascript" rerender="showstate">
        <apex:param name="firstParam" assignTo="{!state}" value="" />
    </apex:actionFunction>

 call the above action function as followed.

 

<apex:outputPanel onclick="methodOneInJavascript(document.getElementById("tsidLabel").innerText)" styleClass="btn"> 
        Click Me 
    </apex:outputPanel>

 Tha value you passed will directly gets assigned to the "state" variable which is controller variable.

abinayaabinaya

Thanks for the reply but I dont want a link option in my page. I am not going to display anything in my page. I am just going to check for the app name. Is there anything like hidden field where i can store my javascript value in hidden field and use that field in my apex class?

 

I need the current app name in my apex class. To achieve this am going in this way.

bob_buzzardbob_buzzard

You can store it in a hidden field, but that would require you to submit a form which equates to an action.    If you are going to do the server round trip, you could use an actionfunction and pass the field as a parameter.

 

Of course there is also the option of javascript remoting - this will still require a round trip to the server, but doesn't involve a form so tends to be faster.

 

If you can explain a little more about your use case, there might be other options.

abinayaabinaya

My task is when a user logs into the application, i need to redirect to different pages based on the logged user role. So i created a visualforce page and set that page as a default landing page. Then in the page action i wrote certain functioanlities to redirect to various tabs based on the user role. I did and it works fine. But i want to check the app name before i could redirect to various tabs.

Hence i got the app name using the following javascript code:

 

var myappname = document.getElementById("tsidLabel").innerTex

 

I am not able to get this app name in the page load of my controller class. Kindly help me out.

bob_buzzardbob_buzzard

I wouldn't expect that to be available, as your Visualforce page is coming out of a different server to the main application.  Thus your visualforce page has no access to the DOM of the containing standard page.

 

You would be able to get at this information from an HTML sidebar component.

DevelopersDevelopers

Hi,

 

I am also want to pass the java script variable to my apex controller on my VF page load.

 

Can any one help me in this how to pass the javascript value to apex controller on page load?

 

Thanks in advance....

 

Regards,

kumar