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

Using js file from static resource in VF page



I have consolidated all my javascript in one js file and stored it in static resource. I am including my jscript file by using includescript tag.


But when i try to access some function from the file it gives an error. Can you please help?


Here is the javascript file: -



function restrictUser(isRestrictedUser,leadId){ if(isRestrictedUser){ alert('you can only convert the leads which you own'); = "/"+leadId; } } function viewAccount(selectedAccountVAlue){ var selectedAccount=document.getElementById(selectedAccountVAlue).value; if(selectedAccount.indexOf('New')!=-1||selectedAccount.indexOf('None')!=-1) alert("You can only view the existing accounts"); else"/"+selectedAccount ); } function viewContact(selectedContactValue){ var selectedContact=document.getElementById(selectedContactValue).value; if(selectedContact.indexOf('New')!=-1||selectedContact.indexOf('None')!=-1) alert("You can only view the existing contacts"); else"/"+selectedContact ); }


 And here is the VF tag: -

<apex:includescript value="{!$Resource.LeadConvert_js}"/>


<apex:selectList id="accountList" value="{!selectedAccountName}" size="1" required="true">
                                <apex:selectOptions value="{!AccountList}" />
                            <apex:commandlink onclick="viewAccount(accountList)"


I even tried 


<apex:selectList id="accountList" value="{!selectedAccountName}" size="1" required="true">
                                <apex:selectOptions value="{!AccountList}" />
                            <apex:commandlink onclick="viewAccount(leadConversionPage:leadConversionForm:leadConversionPageBlock:convertLeadSection:accountSection:accountDropDownSection:accountList)"


but nothing seems to work. Kindly help


Ron HessRon Hess
do you need 

<script> </script>


tags around your file ( top and bottom )



what is the error message?

Shannon HaleShannon Hale

One problem is that you're passing the id "accountList" into viewAccount() here:



<apex:commandlink onclick="viewAccount(accountList)"


 Though you are setting the id of your apex:selectList element to "accountList", Visualforce will generate its own unique id for the element. To access the Visualforce-generated id, you must use $Component.accountList. Also, I think you need to put the value within single quotes, since you appear to be using it as a string in viewAccount():



<apex:commandlink onclick="viewAccount('{!$Component.accountList}')" rerender="hiddenField1">View</apex:commandlink>


 You tried to do something like this in your second example, but you can't rely on that id to remain valid as you add or remove elements from the page.


See if that helps.





Message Edited by shale on 08-07-2009 03:24 PM