difference between console.log / document.write and alert

房东的猫 提交于 2019-12-13 01:09:51

问题


I would know what is the difference between those three lines of code :

console.log("");
document.write("");
alert(""); (or windows.alert("");)

回答1:


console.log("") outputs whatever is passed as the parameter.
e.g. console.log("test") will output "test" to your console

document.write("") adds whatever you want to html.
e.g. document.write("<p>paragraph</p>") will add a new paragraph to a document

alert("") is a popup alert.




回答2:


  1. Developers use console.log() for logging useful info.
  2. document.write modifies what user sees in the browser by adding additional content to DOM.
  3. Alerts are used to alert end users who access the web page.



回答3:


console.log('foo');

will write 'foo' in your debugging console. You can access it via F12 on most browsers or right click on your page and inspect. You should see a "console" panel on the debugging window.

Be careful of what information you dump, it will be shown to every one browsing the page. Some browsers may not like those logs and you could encounter errors on production websites if you forget to remove them.

document.write('foo');

will append 'foo' to the DOM of your current page. This statement is not to be used for debugging purpose.

alert('foo');

will display a popup window to your browser with a single button to close it. "foo" will be the text displayed on the popup. You can use this method to send very important information to the person browsing the page, but try not to abuse of them as they block the visitor.




回答4:


console.log() is used by developers to just debug their code by printing the value inside console.log() in their console...... document.write() is used to add something to the webpage



来源:https://stackoverflow.com/questions/35682997/difference-between-console-log-document-write-and-alert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!