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 6724001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:38:09+00:00 2026-05-26T09:38:09+00:00

I’m not experienced in Javascript, so apologies if this is a basic question, but

  • 0

I’m not experienced in Javascript, so apologies if this is a basic question, but I have a js script in the head of my application that is slowing down the page loading time (e.g. examplepage.php). The code is below:

<script type="text/javascript">
$(document).ready(function(){ 

  $("a").click(function(){

    $.get('getdata.php', function(data) {
            $('#getdata').html(data);
        });
  });

});
</script>

getdata.php runs a script that connects to another service via an API. On examplepage.php I have a link that once clicked activates the js script above which pulls in data from the API (via getdata.php). This feature works, but what I am finding is that on loading the page the js script runs anyway and therefore slows down the loading time of the page.

I don’t want the script to be calling the API unnecessarily (ie. if the user doesn’t click the link on examplepage.php). How can I stop this js script running?

Sorry if this doesn’t make sense – I’m a JS beginner!

Many thanks,

Gregor

  • 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-05-26T09:38:10+00:00Added an answer on May 26, 2026 at 9:38 am

    The script you use is semantically equivalent to this one (I’ve just assigned a name to the anonymous function you use for sake of clarity):

    <script type="text/javascript">
    function readyAction() {
        $("a").click(clickEvent);
    }
    
    function clickEvent() {
        $.get('getdata.php', function(data) {
                $('#getdata').html(data);
            }
    }
    
    $(document).ready(readyAction);
    </script>
    

    You can read it in this way:

    $(document).ready(readyAction); means when the $('document') is full loaded ready (it is a simplification) then execute the function readyAction. Note that the function is addressed (there aren’t the () after his name) and not executed at this line.

    The readyAction function does just a thing: scan the DOM and find all the anchors $("a")
    and then hook to the collected elements an handler clickEvent for the onclick event.

    The function clickEvent do the XHR connection, retrieve the data from server and then fill the div (or whatever the element is) with id getdata. It will be executed only when the user clicks on a link.

    That’s all.

    You are not issuing extra unneeded XHR calls with this code.
    I hope this had shed some light on the inner workings of that code.

    EDIT
    As pointed by Quincy, maybe the DOM you want to manipulate is quite large, or there are a lot of anchor elements and this may slow down your execution, it is a rare chance but maybe it is your case. You can try to restrict the scope of search done by jquery adding an id to the link you want to animate.

    i.e. you can change $("a").click( in $("#loadpagedatalink').click( as far ‘loadpagedatalink’ is the id of the link you have to attach the click handler to.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
this is what i have right now Drawing an RSS feed into the php,
I have some data like this: 1 2 3 4 5 9 2 6
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.