javascript
  • html
  • 2017-08-15 4 views 1 likes 
    1
    document.body.innerHTML = "An error occurred. Please try to login again.<br>"; 
    
    document.body.innerHTML += 'Username: <input type="text" name="username"><br>'; 
    document.body.innerHTML += 'Password: <input type="password" name="password"><br>'; 
    document.body.innerHTML += "<input type='submit' value='Login' onclick='alert(document.getElementById('password').value)'>" 
    

    でスクリプトを注入、このコードを実行して、ログインボタンをクリックした後クロームJavaScriptコンソールはバック与える:はDOM

    SyntaxError: expected expression, got '}'

    は、私はそれを修正できますか?

    答えて

    3

    あなたが引用符を変更してJavaScriptコード内でそれらをエスケープする必要があります:

    document.body.innerHTML = "An error occurred. Please try to login again.<br>"; 
     
    
     
    document.body.innerHTML += 'Username: <input type="text" name="username"><br>'; 
     
    document.body.innerHTML += 'Password: <input type="password" name="password" id="password"><br>'; 
     
    document.body.innerHTML += "<input type='submit' value='Login' onclick='alert(document.getElementById(\"password\").value)'>";

    As for the reason "why the browser throws this specific error?", here is the question:

    コードが間違っているので、ブラウザが実際に次のコードを参照してください。

    (function(event){alert(document.getElementById(
    }) 
    

    ご覧のとおり、 document.getElementById(閉じる{があり、これはあなたのブラウザがあなたに叫んでいるものです。

    +0

    クリックを逃れます。 – WayneXMayersX

    +0

    @SpencerWieczorek実際にはエラーがありました:)そして私は答えを更新しました – Dekel

    +0

    @WayneXMayersX今答えを確認してください – Dekel

    1

    だけで、ログインボタン上の引用符

    document.body.innerHTML = "An error occurred. Please try to login again.<br>"; 
     
    
     
    document.body.innerHTML += 'Username: <input type="text" name="username"><br>'; 
     
    document.body.innerHTML += 'Password: <input type="password" name="password" id="password"><br>'; 
     
    document.body.innerHTML += "<input type='submit' value='Login' onclick='alert(document.getElementById(\"password\").value);return false;' />"

    関連する問題