简体   繁体   English

如何基于窗口宽度加载和运行脚本

[英]How to load and run a script based on window width

I'm working on a 1-page mini-app and I'm trying to load a different version of the the app based on the screen width: 我正在开发一个1页的微型应用程序,并且尝试根据屏幕宽度加载该应用程序的其他版本:

(function() { // This is on the body of the document
    var windowWidth = $(window).width();
    if(windowWidth > 1000){
        $('head').append('<script src=\"http://example.com/jquery.full-width-version.js\" type="text/javascript\"><\/script>');
    }else{
        $('head').append('<script src=\"http:/example.com/jquery.responsive-version.js\" type="text/javascript\"><\/script>');
    }
})(); 

The reason I'm not just using the responsive version is because the full-version has some custom CSS for this client. 我之所以不使用响应版本,是因为完整版为此客户端提供了一些自定义CSS。

Inside of either one of the files there is an object called "IC" with a lot of methods and properties, and a document ready which initiates all the functions. 在这两个文件中的任何一个文件中,都有一个名为“ IC”的对象,该对象具有许多方法和属性,并且已准备好一个文档,用于初始化所有功能。

When I run it I get the message inside of the document ready: 当我运行它时,我已经准备好文档内部的消息:

Uncaught TypeError: Cannot read property 'createConfigurator' of undefined 

In the line: 在该行中:

IC.createConfigurator();

So, it is not recognizing IC as an object 因此,它不是将IC识别为对象

此代码是否在$(document).ready()中?

try using it this way, setting to 'html' a specific size, big and small. 尝试以这种方式使用它,将特定大小(大小)设置为“ html”。

$(function(){
    $('html').data('size','big');
});

$(window).scroll(function (){
    if( $(window).width() > 1000 ){
        if($('html').data('size') == 'big') {
            $('html').data('size','small');
            $('head').append('<script src=\"http://example.com/jquery.full-width-version.js\" type="text/javascript\"><\/script>');
        }
    } else {
        if($('html').data('size') == 'small'){
            $('html').data('size','big');
            $('head').append('<script src=\"http:/example.com/jquery.responsive-version.js\" type="text/javascript\"><\/script>');
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM