您可以将代码放入onload事件中。加载对象后发生onload事件。
请修改您的代码如下:
<head>
<script>
function pageload() {
if (!window.openDatabase) {
alert("Sorry your browser dosent support WebSQL")
document.getElementById("input").disabled = true
document.getElementById("button").disabled = true
} else {
var db = openDatabase("mydb", 1.0, "mydb", 2 * 1024 * 1024)
function execute() {
db.transaction(function (t) {
t.executeSql(document.getElementById("input").value)
console.log(document.getElementById("input").value)
})
}
}
}
</script>
</head>
<body id="body" onload="pageload();">
<textarea id="input"></textarea>
<button onclick="execute()" id="button">Execute SQL</button>
</body>