简体   繁体   English

用户单击按钮时动态加载文件

[英]Dynamically loading files when user clicks button

I am loading files dynamically when a user clicks a button. 当用户单击按钮时,我正在动态加载文件。 There are two different source paths that are hard coded. 硬编码有两种不同的源路径。

When the user clicks the button it should try to load the file using first path. 当用户单击按钮时,它应尝试使用第一个路径加载文件。 If there is an error loading the file, it should try to load the file using the second path. 如果加载文件时出错,则应尝试使用第二条路径加载文件。 If there is an error with the second path it should exit and quit trying and put up a message "File not found". 如果第二条路径存在错误,则应退出并退出尝试,并显示一条消息“找不到文件”。

Here is my code: 这是我的代码:

var bt = $("bt");
var file = "http://www.website.com/files/filename.jpg;
var file2 = "http://www.website.com/files/file2.jpg;

bt.on('click', function(){
    $('#mb').attr('src', file);
    $('#mb').on('error', function(){
          $('#mb').attr('src', file2);
          $('#mb').on('error', function(){
               alert('no files could be found');
               return;
          });
    $('#loadHere').load();
    });
    $('#loadHere').load();
});

it works as long as one of the files are found. 只要找到其中一个文件,它就可以工作。 But, if neither are found it keeps looping because it cannot get out of the first on error function. 但是,如果都找不到,它将继续循环,因为它无法脱离第一个on错误函数。

I really appreciate everyone's help. 我真的很感谢大家的帮助。

Thank you. 谢谢。

I think the easiest way might be an array of image src and good ol recursion when the image fails. 我认为最简单的方法可能是图像src数组和图像失败时的良好ol recursion

Something like this: 像这样:

var bt = $("#bt");
var files = [
    "http://www.website.com/files/filename.jpg",
    "http://www.website.com/files/file2.jpg"
]

bt.on('click', function loadFile() {
    var file = files.shift();

    if (file) {
        $('#mb').attr('src', file);

        // Avoid rebinding onerror with '.one()' (as suggested by @rbyte)
        $('#mb').one('error', function () {
           loadFile(); 
        });
    } else {
        // Throw error  
        $('body').addClass('no-file');
    }
});

Here's a fiddle which demonstrates when both links are broken: 这是一个小提琴,它演示两个链接何时断开:

http://jsfiddle.net/x6pz4txo/ http://jsfiddle.net/x6pz4txo/

And here are two fiddles which demonstrate when both (or just one) link works: 这是两个小提琴,它们说明两个(或一个)链接何时起作用:

http://jsfiddle.net/5sf22hka/ (Both links would work - but just loads the first) http://jsfiddle.net/5sf22hka/ (两个链接都可以工作-但仅加载第一个链接)

http://jsfiddle.net/5sf22hka/1/ (First link broken, second works) http://jsfiddle.net/5sf22hka/1/ (第一个链接断开,第二个作品)

$('#mb').attr('src', file2);

that line makes your infinite loop with onerror event cause you use the same #mb element. #mb您的onerror事件无限循环,导致您使用相同的#mb元素。 You need to different elements for that, but it's better to create new Image for image load waiting 为此,您需要使用不同的元素,但是最好创建new Image以等待图像加载

Try to use 尝试使用

var myImage = new Image,
    myImage2 = new Image;

var count = 0

myImage.src = file
myImage2.src = file2

myImage.onload = function(){}
myImage.onerror = function(){}

myImage2.onload = function(){}
myImage2.onerror = function(){}

not sure what you want to do after files loaded or not, but you may just rewrite your code with Image elements and put myImage2 inside myImage.onload if you want 不知道要在加载文件或不加载文件后要做什么,但是您可以只使用Image元素重写代码,然后将myImage2放在myImage.onload

You should unbind the error instead of the return. 您应该取消绑定错误而不是返回错误。

Try this.. 尝试这个..

var bt = $("bt");
var file = "http://www.website.com/files/filename.jpg;
var file2 = "http://www.website.com/files/file2.jpg;

bt.on('click', function(){
    $('#mb').attr('src', file);
    $('#mb').on('error', function(){
          $('#mb').attr('src', file2);
          $('#mb').on('error', function(){
               alert('no files could be found');
               $('#mb').off('error'); 
          });
    $('#loadHere').load();
    });
    $('#loadHere').load();
});

You need to unbind first, do it before the second on error event is attached. 您需要先解除绑定,然后再附加第二个on error事件。

var bt = $("bt");
var file = "http://www.website.com/files/filename.jpg;
var file2 = "http://www.website.com/files/file2.jpg;

bt.on('click', function(){
    $('#mb').attr('src', file);
    $('#mb').on('error', function(){
          $('#mb').off('error'); 
          $('#mb').attr('src', file2);
          $('#mb').on('error', function(){
               alert('no files could be found');

          });
    $('#loadHere').load();
    });
    $('#loadHere').load();
});

You could use the concept of stack to handle this situation. 您可以使用堆栈的概念来处理这种情况。

var bt = $("bt");
var images = [
        "http://www.website.com/files/filename.jpg",
        "http://www.website.com/files/file2.jpg"
];

bt.on('click', function(){
    $('#mb').attr('src', images.shift());
    $('#mb').on('error', function(){
        if (images.length) {
            $('#mb').attr('src', images.shift());
        } else {
            alert('no files could be found');
            return;
        }
        $('#loadHere').load();
    });
    $('#loadHere').load();
});

You keep an array with all the images you want to try to load. 您保留了一个数组,其中包含要尝试加载的所有图像。 Once the button to load the images is clicked, you keep trying to load all the images in the array, until either one of those images exists or there's no more images to load. 单击加载图像的按钮后,您将继续尝试加载阵列中的所有图像,直到这些图像之一存在或没有更多图像要加载。

This way you don't have to bind an error event more than once or call recursive functions. 这样,您不必多次绑定错误事件或调用递归函数。

Hope it helps. 希望能帮助到你。

This is what I used to fix the issue: 这是我用来解决此问题的方法:

var bt = $("bt");
var images = [
        "http://www.website.com/files/filename.jpg",
        "http://www.website.com/files/file2.jpg"
];

bt.on('click', function(){
    $('#mb').attr('src', images.shift());
    $('#mb').on('error', function(){
        if (images.length) {
            $('#mb').attr('src', images.shift());
        } else {
            alert('no files could be found');
            return;
        }
        $('#loadHere').load();
    });
    $('#loadHere').load();
});

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

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