0

I want to load .js file before the </body> in footer only if div with particular class name exists on page. I found the div with class name using if condition. But I am not getting how to add script in footer of page with it.

Here is my if condition code

if ($("#world-map-markers")[0]){

} else {
    // Do something if class does not exist
}

I want to add this in static html page. There is no backend language.

Thanks

Liam
  • 27,717
  • 28
  • 128
  • 190
Miren
  • 11
  • 1

2 Answers2

0

Try below code

<script type="application/javascript">
function loadJS(file) {
    // DOM: Create the script element
    var jsElm = document.createElement("script");
    // set the type attribute
    jsElm.type = "application/javascript";
    // make the script element load file
    jsElm.src = file;
    // finally insert the element to the body element in order to load the script
    document.body.appendChild(jsElm);
}
</script>
programtreasures
  • 4,250
  • 1
  • 10
  • 29
0

You may can do somthing like this.

if ($("#world-map-markers")[0]){
    $('footer').append('<script type="text/javascript" src="js/load.js"></script>');
} else {
    // Do something if class does not exist
}
maulik
  • 919
  • 7
  • 22