You need to sign in to do that
Don't have an account?
Using js file from static resource in VF page
Hi,
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'); window.top.location = "/"+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 window.open("/"+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 window.open("/"+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:selectList>
<apex:commandlink onclick="viewAccount(accountList)"
rerender="hiddenField1">View</apex:commandlink>
I even tried
<apex:selectList id="accountList" value="{!selectedAccountName}" size="1" required="true">
<apex:selectOptions value="{!AccountList}" />
</apex:selectList>
<apex:commandlink onclick="viewAccount(leadConversionPage:leadConversionForm:leadConversionPageBlock:convertLeadSection:accountSection:accountDropDownSection:accountList)"
rerender="hiddenField1">View</apex:commandlink>
but nothing seems to work. Kindly help
<script> </script>
tags around your file ( top and bottom )
what is the error message?
One problem is that you're passing the id "accountList" into viewAccount() here:
<apex:commandlink onclick="viewAccount(accountList)"
rerender="hiddenField1">View</apex:commandlink>
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.
--Shannon