A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://stackoverflow.com/questions/49572604/loading-a-js-file-conditionally-how below:

javascript - Loading a .js file conditionally. How?

Asked 7 years, 4 months ago

Viewed 211 times

First demo is a .js file which is loaded with:
<script src="https://cdnjs.cloudflare.com/ajax/libs/zooming/1.4.2/zooming.js"></script> https://codepen.io/zuhocuyixu/pen/oqpdWz

The .js adds a nice zooming feature to the image.

Second demo is trying to load the same .js file with head.js. It does not work. https://codepen.io/zuhocuyixu/pen/XEVEwG


Any advice on why this is not working?

asked Mar 30, 2018 at 10:18

If you look at your dev tools, you will find zooming is actually loading.

Your problem is zooming is loaded after your document html has been loaded, so you will need to tell zooming to re-parse,.

I don't personally use Zooming, but try ->

head.load('https://cdnjs.cloudflare.com/ajax/libs/zooming/1.4.2/zooming.js', function () {
  new Zooming();
});

answered Mar 30, 2018 at 10:35

KeithKeith

24.3k22 gold badges3131 silver badges4848 bronze badges

I recommend looking through the zooming.js docs

You can initialize the zooming on images like this yourself:

head.ready(document, function() {
    head.load('https://cdnjs.cloudflare.com/ajax/libs/zooming/1.4.2/zooming.js', function() {
         var zooming = new Zooming();
         zooming.listen('.my-img');
    });
});

and change your html to this:

<img 
    class="my-img"
    style='max-width: 100%;'
    draggable="false" ondragstart="return false;"
    src="https://image.ibb.co/bxRTKn/trees_crop.jpg"
    data-original="https://preview.ibb.co/eRYm5S/trees.jpg"
    alt="test"
/>

answered Mar 30, 2018 at 11:02

pascalpuetzpascalpuetz

5,42811 gold badge1515 silver badges2626 bronze badges

1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4