How to load a plugin according to the width of the screen?

4

Hello, my problem is the following, I have two Plugins with similar functions, however they can not be loaded at the same time because errors occur.

Is there any way to load a particular script according to your browser's resolution?

Ex:

<script>
    var largura = window.innerWidth;

    if(largura > X){
        //carregar plugin 1
        //<script type="text/javascript" src="_js/plugin1.js"></script>
    }

    else{
        //carregar plugin 1
        //<script type="text/javascript" src="_js/plugin2.js"></script>
    }
</script>
    
asked by anonymous 17.11.2015 / 02:11

1 answer

5

Vinicius, here's an answer that might work:

 <script>
  function verifySize(){
    var largura = window.innerWidth;
    // Cria  a tag script
    script = document.createElement("SCRIPT");

    if(largura > 900){
      //carregar plugin 1 mudando o src pro script desejado
      script.src = "_js/jquery.fullPage.js";
    }else{
      //carregar plugin 2 mudando o src pro script desejado
      script.src = "_js/jquery.fullPage.js";
    }
    //Carrega o script dentro do body
    document.body.appendChild(script);
  }
  // Caso você queira que o pluggin seja carregado somente ao entrar na pagina use o onload
  window.onload = verifySize;
  // Caso você queira que o pluggin seja carregado ao longo de mudanças dinâmicas no tamanho da janela use o onresize.
  window.onresize = verifySize;
</script>
    
17.11.2015 / 02:31