My jQuery code is not kicking off in DisplayForm in SharePoint
-
10-10-2020 - |
Question
I have added some jQuery code to the DisplayForm as follow-
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script type='text/javascript'>
function Execute_Attachments_Processing(){
jQuery('table#idAttachmentsTable td a').each(function(){
var $parent = jQuery(this).parent();
var a_src = jQuery(this).attr('href');
var a_text = jQuery(this).text();
if(a_src.contains('jpg') || a_src.contains('jpeg') || a_src.contains('png') || a_src.contains('gif')){
jQuery(this).remove();
$parent.append('<div style="text-align:center; padding-bottom:20px;"><a target="_blank" href="'+a_src+'"><img title="'+a_text+'" src="'+a_src+'" width="300px" /><br/>'+ a_text+'</a></div>');
}
});
}
jQuery(document).ready(function(){
Execute_Attachments_Processing();
});
</script>
// Other html/xml code here..
</content>
It converts attachments into viewable images.
It is not executing at all. What could be the problem?
Solution
Use $(window).load(function() {});
instead of document.ready()
. It seems your DOM is loading after you are calling the function.
window.load
will run after all DOM has been loaded completely.
OTHER TIPS
You might want to use _spBodyOnLoadFunctionNames.push
method instead of jQuery's document.ready()
, read this article.
Also, other functions available.
If you are on SharePoint 2013/Office 365 check if minimum download strategy is enabled for the site. Disable it and have a try.
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange