You need to sign in to do that
Don't have an account?
Savvy Admin
Make included JavaScript file work with jQuery
I have included jQuery as Static Resource and it is working with the Visualforce page when I include my own JavaScript code in <script type="text/javascript"> tag.
However, if I copy/paste the code into a .js file and upload it on static resource and include the file as apex:includeScript value, the JavaScript code never worked.
On the Visualforce page, I wrote jQuery.noConflict(); and use jQuery instead of '$' to make it work.
Later when I copied the original code into a .js file, I did not change anything. It did not work. Then I tried to use something like var j$ = jQuery.noConflict(); and replace all jQuery by j$. Not working. I also tried to delete the noConflict line and all using '$' as selector. Not working. Another try is to add (function($) { }) (jQuery); to wrap the original code. Still not working.
Is there anything that I am mising to make the external js file which references jQuery working with the visualforce page?
However, if I copy/paste the code into a .js file and upload it on static resource and include the file as apex:includeScript value, the JavaScript code never worked.
On the Visualforce page, I wrote jQuery.noConflict(); and use jQuery instead of '$' to make it work.
Later when I copied the original code into a .js file, I did not change anything. It did not work. Then I tried to use something like var j$ = jQuery.noConflict(); and replace all jQuery by j$. Not working. I also tried to delete the noConflict line and all using '$' as selector. Not working. Another try is to add (function($) { }) (jQuery); to wrap the original code. Still not working.
Is there anything that I am mising to make the external js file which references jQuery working with the visualforce page?
1. So, I'll start by saying that one possible reason this is working when it's on your page and not when it's in a resource is that merge field you have on line 6 when you declare This will properly resolve when on the Visualforce page but not in the resource. To use something like this, throw a script at the top of your page like this: Now you have an object literal populated with the actual values of your merge fields that can be used in your resource file. So now, within your .resource, you'd say 2. What is the component that "loanDetails" is the id attribute for? If it's a visualforce component, such as <apex:outputPanel id="loanDetails" layout="block">, the id is not loanDetails at runtime. So in this case, $("#loanDetails") is actually returning null, and not the jQuery object that you are expecting it to. As in the above, use console to explore this type of scenario, by directly executing $("#loanDetails") in the console. It will print out the DOM element, if any, that it finds for you so you can see what it's actually doing.
If you've found that neither of these reasons are the solution to the issue you're seeing, then post your markup and we can dig deeper.
All Answers
I actually received nothing about errors. Just the JavaScript code does not make effects on the page.
For the script, nothing special. I have included the changes I made for the two situations. I can give a chunk of the script for the original code I wrote in <script type="text/javascript"> tag. Later, I only included all the code in .js file and put it on static resources and it did not work.
Example of my script:
<script type="text/javascript">
jQuery.noConflict();
jQuery(document).ready(function() {
<!-- Step 1 Loan Details -->
var loanDetails = document.getElementById("{!$Component.OppApplication:AppForm:App:LoanDetails}");
jQuery("#loanDetails").addClass('active');
jQuery("#loanDetails").click(function () {
if (jQuery(this).hasClass('active')) {
jQuery(loanDetails.childNodes[2]).slideUp ();
jQuery(loanDetails.childNodes[3]).slideUp ();
jQuery(this).removeClass('active');
jQuery(this).addClass('unactive');
} else {
jQuery(loanDetails.childNodes[2]).slideDown ();
jQuery(loanDetails.childNodes[3]).slideDown ();
jQuery(this).removeClass('unactive');
jQuery(this).addClass('active');
}
});
});
</script>
1. So, I'll start by saying that one possible reason this is working when it's on your page and not when it's in a resource is that merge field you have on line 6 when you declare This will properly resolve when on the Visualforce page but not in the resource. To use something like this, throw a script at the top of your page like this: Now you have an object literal populated with the actual values of your merge fields that can be used in your resource file. So now, within your .resource, you'd say 2. What is the component that "loanDetails" is the id attribute for? If it's a visualforce component, such as <apex:outputPanel id="loanDetails" layout="block">, the id is not loanDetails at runtime. So in this case, $("#loanDetails") is actually returning null, and not the jQuery object that you are expecting it to. As in the above, use console to explore this type of scenario, by directly executing $("#loanDetails") in the console. It will print out the DOM element, if any, that it finds for you so you can see what it's actually doing.
If you've found that neither of these reasons are the solution to the issue you're seeing, then post your markup and we can dig deeper.
To access children, you must do something like either of the following:
1. Use .children(): https://api.jquery.com/children/
The above line returns a collection of jQuery elements which are children of the element returned by $("#id"). You can subsequently do something like:
2. Use .find(): https://api.jquery.com/find/
The above line will return descendants of $("#id") that match the given selector. For example, let's say you wanted to retrieve all of the <tr> elements within a <table>:
We can then subsequently do something similar as in the above when we followed up with .each().
Now I am working on other projects. When I have time, I will make testings on this part and get back to you about this issue ASAP.
var loanDetails = document.getElementById("{!$Component.OppApplication:AppForm:App:LoanDetails}");
and need to be fixed by the method Matt provided
window.__VARS_FROM_MERGE_FIELDS__ = {
"loan_details" : "{!$Component.OppApplication:AppForm:App:LoanDetails}"
};
Later, when I have time, I will get back to have a try.