Simple script, but could I have done it better.
I developed a very small script to look through a sharepoint announcement for attachment links, then convert them to thumbnails when the page loads. It all works, but I'm looking for feedback on what could have been done better:
HTML (don't have much control over it, sharepoint):
- <div style="width:100%;display:block;overflow:auto;" class="imthumbs"></div>
- <div style="padding: 6px 4px;width:100%;" class="imattach"><b>Attachments:</b><br/>
- <div style="margin-left:12px;margin-top:5px;">
- <TABLE border=0 cellpadding=0 cellspacing=0 id=idAttachmentsTable>
- <TR><TD class="ms-vb"><span dir="ltr"><a tabindex=1 href="/968/Mucker 2015 Adam.jpg">Mucker 2015 Adam.jpg</a></span></TD></TR>
- <TR><TD class="ms-vb"><span dir="ltr"><a tabindex=1 href="/968/Mucker 2015 Beta Amy.jpg">Mucker 2015 Beta Amy.jpg</a></span></TD></TR>
- <TR><TD class="ms-vb"><span dir="ltr"><a tabindex=1 href="/968/Mucker 2015 Cathy.jpg">Mucker 2015 Cathy.jpg</a></span></TD></TR>
- </TABLE>
- </div>
- </div>
and the jquery:
- $(document).ready(function(){
-
- $(".imattach").find("a").each(function(){
- var url = $(this).attr('href');
- if (url.toLowerCase().indexOf(".jpg") >= 0 || url.toLowerCase().indexOf(".gif") >= 0 || url.toLowerCase().indexOf(".png") >= 0)
- {
- var count = $(this).closest(".imattach").find("a").length;
- if (count < 2)
- {
- var imglink = '<a href="' + url + '"><img src="' + url + '" class="thumbsingle"/></a>';
- $(this).closest(".imcont").find(".imbody").prepend(imglink);
- }
- else
- {
- var imglink = '<a href="' + url + '"><img src="' + url + '" class="thumbinline"/></a>';
- $(this).closest(".imcont").find(".imthumbs").append(imglink);
- }
- }
- });
-
- });
Looking to get better, thanks a lot!