Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:06:35+00:00 2026-06-11T07:06:35+00:00

The following script works well to both truncate a link and add an icon

  • 0

The following script works well to both truncate a link and add an icon class BUT,
It was too slow: using document.ready to hold the code from executing UNTIL all the elements on a page loaded.

I removed it, AND it works BUT it won’t work on the last element

<script>

         jQuery.noConflict();

         jQuery(".resultAction").each(function(){ 

    var fileName =  jQuery(this).find('a').html()
    var fileExtension = fileName.substring(fileName.lastIndexOf('.') );
    var nameChars= fileName.length;
    var shorter=fileName.substring(0,10)+"...";
           //apply style to relative to file extension
        if(fileExtension=="jpg"||"pdf"||"mov"){
        jQuery(this).find("#indicator").addClass("is" + fileExtension.slice(1) );
        }
           //no file extension hide the icon div "indicator"
        if((fileExtension.indexOf('.') == -1)){
        jQuery(this).find("#indicator").addClass("not");
        }
            //truncate text
        if(nameChars>10){
            jQuery(this).find('a').text(shorter);
        }
         });


    </script>

so I got rid of the .ready above and added this over again:

<script>
jQuery(document).ready(function(){
jQuery.noConflict();
jQuery(".resultAction:last").each(function(){ 
//-SAME CODE ABOVE-....
</script>

-notice the :last -which of course works -but it’s redundant and I was wondering if there is a more efficient way to both make this code run instantly (as the page loads each element) and completely instead of running two identical scripts one to run fast, and one to apply to the last item it missed.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:06:37+00:00Added an answer on June 11, 2026 at 7:06 am

    Common code can be put in a function and called from both places you use it, thus only having one copy of the code, but called from multiple places.

    jQuery(".resultAction:last").each(processResult);
    

    and

    jQuery(".resultAction").each(processResult);
    

    As for speeding up jQuery(".resultAction:last"), there are some options:

    1. Give the last one an id and fetch it directly using the id.
    2. Instead of running it on domReady, run it from a script tag right at the end of the <body>. This will execute a little sooner than domReady and be just as safe.
    3. Do your query on jQuery(".resultAction") just once and save that result. You can then use the whole list or the last one at any time without any further DOM lookups.
    4. If speed is really an issue, jQuery is often not the fastest way to do things (though the speed differences are usually not noticed). For example, document.getElementById("item") is more than 10x faster than jQuery("#item").

    Also, something is probably wrong with this code:

    jQuery(this).find("#indicator")
    

    There should only be one #indicator id in your entire document so you should not need to confine it to the this context. So, either you can just use $("#indicator") to get the one object like that or you need to be using a class instead of an id because there are multiple objects like this in the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ORIGINAL QUESTION: I have the following script: http://jsfiddle.net/mnXdv/12/ It works well, but I need
I have the following script that works well in Firefox and Chrome (not sure
The following script works for me, but I wonder if it could be done
The following script works to open Firefox's location/awesome bar from anywhere using control-l ,
The following script works fine: $(#regform).validate().showErrors({username:message}); After I changed the script to the below
I have the following code that works fine in IE: <HTML> <BODY> <script language=JavaScript>
I have the following chunk of code. It works perfectly. <div id=restaurant_locations></div> <script type=text/javascript>
I have a following script: $(document).ready(function() { $('#q1_6').on(click, function(event){ $('#q1-f').slideToggle(350); }); $('#q1_7').on(click, function(event){ $('#q1-m').slideToggle(350);
I am using the following script to export a list of our products to
I have a varable var file = "testfile.txt"; I am using the following script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.