0
I have a jquery function responsible for cutting a photo when a photo upload takes place:
document.addEventListener('DOMContentLoaded', function() {
function readImage() {
if (this.files && this.files[0]) {
var file = new FileReader();
file.onload = function(e) {
document.getElementById("preview").src = e.target.result;
};
file.readAsDataURL(this.files[0]);
}
}
document.getElementById("file").addEventListener("change", readImage, false);
var x = document.getElementById("myDIV");
$("[type=file]").on("change", function() {
var file = this.files[0];
var formdata = new FormData();
formdata.append("file", file);
if (file.name.length >= 30) {
$('#name span').empty().append(file.name.substr(0, 30) + '..');
} else {
$('#name span').empty().append(file.name);
}
if (file.size >= 204800) {
$('#preview').hide();
$('#imgpadrao').show();
} else {
$('#imgpadrao').hide();
$('#preview').show();
$('.img-result').removeClass('hide');
}
var ext = $('#file').val().split('.').pop().toLowerCase();
if ($.inArray(ext, ['png', 'jpg', 'gif', 'bmp']) == -1) {
$('#info').hide();
$('#size').hide();
$('#labelfile').text('Escolha uma foto');
$('#file').val('');
$('#preview').hide();
alert('Essa extensão de foto não é permitida!');
}
});
//funcao crop:
var cropper = '';
$("#file").change(function(e) {
if (e.target.files.length) {
var file = this.files[0];
if (file.size >= 204800) {
$('.result').addClass('hide');
$('#botaocortar').addClass('hide');
alert('Esta imagem excedeu o tamanho máximo permitido (200kb)');
} else {
var reader = new FileReader();
reader.onload = function(e) {
var img = document.createElement("img");
img.id = 'image';
img.height = '300'
img.src = e.target.result;
$('.result').html('');
$('.result').removeClass('hide');
$('.result').append(img);
$('#botaocortar').removeClass('hide');
$('.options').removeClass('hide');
cropper = new Cropper(img);
};
reader.readAsDataURL(e.target.files[0]);
}
}
})
$('#botaocortar').click(function(e) {
e.preventDefault();
var imgSrc = cropper.getCroppedCanvas({
width: $('.img-w').val()
}).toDataURL();
$('.cropped').removeClass('hide');
$('.img-result').removeClass('hide');
$('.cropped').attr('src', imgSrc);
$('.download').removeClass('hide');
$('.download').attr('download', 'imagename.png');
$('.download').attr('href', imgSrc);
})
});
The problem occurs that when I change route and then return to the page where the script should be executed, the function is not executed, it is only executed after I refresh the page. Does anyone have any idea what might be going on?
Why are you wearing the event
DOMContentLoaded
?– Thiago Magalhães
after I realized this problem where my function only worked after reloading the page, I started to transform this function from jquery to javascript... I haven’t finished yet, I expect the problem isn’t that
– veroneseComS
Already tested to use
$().ready(function () {
in place ofdocument.addEventListener('DOMContentLoaded', function() {
?– Thiago Magalhães
yes, it was what was before the domcontentloaded, I believe the problem is another
– veroneseComS
Leticia, can you insert a demonstration of what you’re doing in the Code Snippet? It’s easier to help.
– Inácio Régis
found some people who are with this problem too, I believe I’ll have to remove the script and add again every time in the class constructor
– veroneseComS