asotshia
asotshia

Reputation: 121

Alternative of this script in jQuery?

I use the following script to get the content of the remaining.php.
The drawback is that sometimes it doesn't work or it is kinda slow to display the text. Is there any other way of doing this ?

Thank you

$(document).ready(function(){
    $("#SubmitButton").click(function (){
        $('#remaining').load('remaining.php');
    });
});

Upvotes: 0

Views: 74

Answers (4)

Leo
Leo

Reputation: 5286

If "sometimes it doesn't work or it is kinda slow", the problem is probably the server you are using, not your javascript code.

The javascript code you're showing us here doesn't really do anything that could be slow, it only binds an event on a submit button. However, what could be slow is waiting for the answer from your web server when sending a request for remaining.php

From there, there is a thousand of reasons why your web server could be slow. Maybe you could post the content of your remaining.php file so we can see what is going on in there.

Upvotes: 1

Dmitry Traytel
Dmitry Traytel

Reputation: 111

When you're using .load(), you're sending a request to the server to get your content, which is why it can seem slow. I'm not sure why it sometimes won't work , but I would venture to guess that you may be clicking $("#SubmitButton") before $(document).ready fires.

Depending on your implementation, you may be able to refactor your application so that the text you want to display is pre-loaded on the page.

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could directly include the contents of remaining.php into the initial markup but make it hidden by applying display:none; style to the #remaining element. Then when the button is clicked simply show it:

$(function() {
    $('#SubmitButton').click(function () {
        $('#remaining').show();
    });
});

Of course if you need to pass some parameters to the script which will depend on some javascript variables that are known only at the moment the button is clicked you will need to use AJAX as you are currently doing.

Upvotes: 1

CassOnMars
CassOnMars

Reputation: 6181

This isn't really a fault of jQuery, but the speed of return from your server. Perhaps there's a better way to handle it instead of fetching a full page?

For example, if your content request was only retrieving a message, you could return JSON from your server and have jQuery handle the data:

$(document).ready(function(){
    $("#SubmitButton").click(function (){
        $.post('remaining.php',
               null,
               function(data) {
                   // do stuff with your JSON result
               });
    });
});

Upvotes: 0

Related Questions