页面加载后的JavaScript函数调用不工作

JavaScript function call after page loading is not working

本文关键字:函数调用 工作 JavaScript 加载      更新时间:2023-09-26

基本思路是登录一个页面,点击一个书签

我正在尝试创建一个chrome书签,它应该

  1. 加载页面
  2. 填写详细信息(调用fillDetails()函数实现)
  3. 提交表格

javascript:(function(){
window.location ="http://www.example/login.html";
window.onload = fillDetails();
function fillDetails() { 
    if(document.URL.indexOf("http://www.example/login.html") > -1){
        document.getElementById("username").value="uname";
        document.getElementById("password").value="pass";
        document.getElementById("login").submit();
        console.log("Success");
    } else {
        console.error("Cannot Insert values");
    }
}})();
误差

fillDetails()在页面加载之前执行,但它应该在页面加载

之后执行。

您将window.onload设置为fillDetails()执行的结果,而不是将其设置为函数本身。应该是:

window.onload = fillDetails;