0

I have an image scroller that is blocking resources from loading until it has loaded. I have tried deferring the script but it then doesnt want to work when its deferred. What would be the simplest method for getting it to load after the rest of the page? Ive looked at some jquery methods but its like reading chinese to me

Techagesite
  • 285
  • 4
  • 14
  • 2
    `its like reading chinese to me`. Time to start learning the language, then. – Jonathon Reinhart May 20 '14 at 06:51
  • `$(document).ready(function() {... //stuff happens here});` maybe? – sideroxylon May 20 '14 at 06:55
  • Try using $.getScript(); function. Call this on DOM ready and its success callback try to call the image scroller functions you need. Try using these links http://api.jquery.com/jquery.getscript/ and http://www.w3schools.com/jquery/ajax_getscript.asp – SSS May 20 '14 at 06:56
  • @sideroxylon is that all i need to use? – Techagesite May 20 '14 at 06:56
  • From the jquery api doc: 'The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code.'. Give it a try and see. – sideroxylon May 20 '14 at 07:00
  • It would be useful to understand why it doesn't work when deferred because all other methods of delaying it are likely to suffer similar consequences. – jfriend00 May 20 '14 at 07:01
  • @sideroxylon Is it as simple as putting that piece of code you gave me on the page or is more required? – Techagesite May 20 '14 at 07:03
  • @jfriend00 i wouldnt know how to find that out. all i know is it works without the defer tag but not with. – Techagesite May 20 '14 at 07:04
  • 2
    @Techagesite I checked the below links and they are in English as well. So You wont have problem understanding those. Try to get that working with some efforts on your end. http://api.jquery.com/ready/ and http://www.w3schools.com/jquery/event_ready.asp – SSS May 20 '14 at 07:05
  • @jfriend00 you can have a look at the page, maybe you can spot something http://www.techagesite.com/page-1work1112211.htm its not currently deferred – Techagesite May 20 '14 at 07:06
  • When I got to that web page, what am I looking for? What script? What problem that you're trying to fix? – jfriend00 May 20 '14 at 07:10
  • In the document head theres a script and right before the closing body there is the script for the scroller – Techagesite May 20 '14 at 07:12
  • 1
    @Techagesite - Actually you have two separate script blocks that are using the scroller code, not just the one at the end. – jfriend00 May 20 '14 at 07:26
  • unhold my question. Im getting answers from people who understand the question and making progress – Techagesite May 20 '14 at 07:59
  • How is the question unclear? – Techagesite May 20 '14 at 08:01

2 Answers2

3

Here are some various options:

  1. Place the <script> tag right before the </body> tag. This will allow the rest of the DOM to load before your script even starts to load.

  2. Construct some code to dynamically load the script and don't run that code until either $(document).ready() fires or perhaps even $(window).load() fires depending upon how many resources you want to wait for before starting your script. You can dynamically load the script in jQuery with $.getScript() or it's fairly simple to just dynamically insert a script tag too.

  3. Troubleshoot your code to figure out why the defer attribute doesn't work because it's designed for situations like yours where you want other things to load first. My guess this was because you added defer loading for the library, but didn't delay your own code that attempts to use the library thus that code didn't work when you deferred the loading of the library.

Some references on script loading:

load and execute order of scripts

Script Tag - async & defer

improving website performance by dynamically loading javascript?

pure JavaScript equivalent to jQuery's $.ready() how to call a function when the page/dom is ready for it


In looking at your actual page code, a reason why your script wouldn't work when you add defer to it is that you have code that depends on that script that can't be run until after the dynamic scroll code has been loaded. In looking at your code, I see this block of code and one other block similar to it:

        <script type="text/javascript">

if ( DYN_WEB.Scroll_Div.isSupported() ) {

    DYN_WEB.Event.domReady( function() {

        // arguments: id of scroll area div, id of content div
        var wndo = new DYN_WEB.Scroll_Div('wn', 'lyr1');
        // see info online at http://www.dyn-web.com/code/scrollers/continuous/documentation.php
        wndo.makeSmoothAuto( {axis:'h', bRepeat:true, repeatId:'rpt1', speed:100, bPauseResume:true} );

        var wndo2 = new DYN_WEB.Scroll_Div('wn2', 'lyr2');
        wndo2.makeSmoothAuto( {axis:'h', bRepeat:true, repeatId:'rpt2', speed:60, bPauseResume:true} );

    });
}

        </script>

Both of these have to be run AFTER the scroll library is loaded. So, if you delay the loading of the scroll library, then this code has to be run after the library is loaded.

If you're moving the scroll library to right before </body>, then place these blocks of code right after it (without using the defer tag on any).

Community
  • 1
  • 1
jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • if i put it at the end of the document do i just insert it like this – Techagesite May 20 '14 at 07:10
  • 1
    @Techagesite - Yes, you would move that to the end of the `` section, but you would also have to move your DYN_WEB code that uses that library to be AFTER wherever you insert this script tag (see what I added to the end of my answer). – jfriend00 May 20 '14 at 07:19
0

Try this:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(window).bind("load", function() {
   // code here
});
</script>

or this:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(window).bind("load", function() {
   if ( DYN_WEB.Scroll_Div.isSupported() ) {

DYN_WEB.Event.domReady( function() {

    // arguments: id of scroll area div, id of content div
    var wndo = new DYN_WEB.Scroll_Div('wn', 'lyr1');
    // see info online at http://www.dyn-web.com/code/scrollers/continuous/documentation.php
    wndo.makeSmoothAuto( {axis:'h', bRepeat:true, repeatId:'rpt1', speed:100, bPauseResume:true} );

    var wndo2 = new DYN_WEB.Scroll_Div('wn2', 'lyr2');
    wndo2.makeSmoothAuto( {axis:'h', bRepeat:true, repeatId:'rpt2', speed:60, bPauseResume:true} );

});
}
});
</script>
sideroxylon
  • 3,977
  • 1
  • 18
  • 33