0

I have an external JavaScript src which I want to add a loading animation until it's finish loading:

<script src="https://xxxx.domain.com/xxx.js" type="text/javascript"></script>

I'm currently using jQuery (window).load, but its waiting until all page is fully loaded, I want to wait only for that specific code:

<script>$(window).load(function(){$(".loadingif").hide();});</script>

Update: This is my code as you have suggested, it's is not working, what I'm doing wrong---

some text.....
<span class="loading-gif"></span>
<script src="https://xxxx.domain.com/xxx.js" type="text/javascript"></script>
<script>hideLoading();</script>
some text.....
<script>function hideLoading(){$(".loading-gif").hide();}</script>
3
  • Can you edit the code that needs to load? Commented Oct 11, 2016 at 12:27
  • no, I don't have control over it Commented Oct 11, 2016 at 12:28
  • Can you control your page enough to run a specific JS block directly after that file has been included? Commented Oct 11, 2016 at 12:30

2 Answers 2

3

Hopefully this works:

<script src="https://xxxx.domain.com/xxx.js" type="text/javascript"></script>
<script type="text/javascript">
    hideLoadingThingy();
</script>

The second script should run after the first one finishes loading. This is because it is included after the first one, so the first one is loaded first.

Unless the methods in xxx.js are called asynchronously, your browser will only execute one task at a time. Read more about that here

Update:

Use this:

some text 2.....
<span class="loading-gif"></span>
<script src="https://xxxx.domain.com/xxx.js" type="text/javascript"></script>
<script type="text/javascript">$(".loading-gif").hide();</script>
some text 2.....
Sign up to request clarification or add additional context in comments.

7 Comments

That's what I was getting at with my comment, so +1, but an explanation of why this works would be good.
@JamesThorpe Yeah, I noticed your comment as I was writing my answer. :)
@Feathercrown I did as you suggested but it is not working, what am i doing wrong?
@Dorel You have to replace hideLoadingThingy() with whatever code hides your loading thingy. In your case, this would be hideLoading().
@Feathercrown I have done as you suggested, the image is not showing, are you sure that second script will run after the first one finishes loading?
|
0

You could key off of the expected javascript object that will be loaded into global scope:

(function checkValue(){
    if (libraryValue === undefined){
        setTimeout(checkValue, 100);
    } else {
        $(".loadingif").hide();
    }
})();

I would use Feathercrown's solution unless you choose to load that library asynchronously.

3 Comments

I was thinking of something like this, but it slows down the website more than my solution.
Checking a javascript value in memory 10 times a second is trivial.
Thanks for the mention! That asynchronous point is a good one though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.