window.onload 如果错误 - 需要语句

window.onload if error - statement expected

window.onload(
    if(localStorage.getItem('name') !== null) {
        $('#greet').html('Hello ' + localStorage.getItem('name'))
    };
);

为什么我在此处的 if 上收到声明预期错误?我不能把 if-s 放在 window.onload 中吗?

你的代码是错误的,这是写的。因为您应该将函数分配给 window.onload。 阅读文档:https://developer.mozilla.org/en/docs/Web/API/GlobalEventHandlers/onload

window.onload = function() {
  if (localStorage.getItem('name') !== null) {
    $('#greet').html('Hello ' + localStorage.getItem('name'));
  }
};