javascript - Correctly usage of vars in javascript
问题描述
How can I use these both scripts on the same page? I'm new in js but I'm pretty sure that I can't use these same vars to make both scripts work correctly after an error (or even more if I want to). "Swal" is a customized alert.
<script>
window.onload = function(){
var url = new URL(window.location.href);
var error = url.searchParams.get("error");
if(error==1) swal("Oops...", "Primeiro você deve escolher uma foto. Clique no botão 'Alterar Foto'.", "error");
}
</script>
<script>
window.onload = function(){
var url = new URL(window.location.href);
var error = url.searchParams.get("error");
if(error==2) swal("Formato Inválido", "Por favor, escolha uma imagem no formato 'PNG' ou 'JPG'.", "warning");
}
</script>
解决方案
不要使用旧的 onload/onclick,如果需要支持 < IE9,请使用 addEventListener 和/或 attachEvent。
推荐阅读
- laravel - Laravel:从队列中删除作业
- javascript - 如何在打字稿中定义 javascript 对象
- php - str_to_date 函数在复杂的 mysql 查询 joomla 中不起作用
- android - Firebase:将分析和崩溃数据报告到不同的项目
- javascript - 使用 JavaScript 从对象中删除
- mysql - 分组并拥有 MYSQL
- ruby-on-rails - Rails ActiveAdmin 为 PublicActivity gem 创建资源
- python - 变量检查器(来自 nb 扩展)在 Jupyter Notebook 5.6.0 中不起作用
- javascript - 在选择选项更改时隐藏/显示 div 不起作用
- java - JavaFX 在鼠标悬停时显示一个窗格