mdn / content

The content behind MDN Web Docs
https://developer.mozilla.org
Other
9.24k stars 22.49k forks source link

Remove alert() from live samples #7566

Open wbamberg opened 3 years ago

wbamberg commented 3 years ago

window.alert() doesn't work cross-origin in Chrome any more, meaning that any of our live samples that rely on alert() are now broken.

See also https://github.com/mdn/content/issues/7553.

This is (I think) all the live samples in Web/API that use alert() in a live sample (list now maintained by @Josh-Cena):

(I might have missed some).

In addition, the following ones use prompt() and are also broken:

We should replace all of these with some alternative. A common alternative is to write the output into an element in the example's document.

wbamberg commented 3 years ago

This is the complete list of pages under Web/API that contain a <pre class="js" block that contains alert(:

https://developer.mozilla.org/en-US/docs/Web/API/animation/ready https://developer.mozilla.org/en-US/docs/Web/API/attr/localname https://developer.mozilla.org/en-US/docs/Web/API/canvas_api/tutorial/applying_styles_and_colors https://developer.mozilla.org/en-US/docs/Web/API/canvas_api/tutorial/basic_animations https://developer.mozilla.org/en-US/docs/Web/API/canvasrenderingcontext2d/addhitregion https://developer.mozilla.org/en-US/docs/Web/API/clipboard/read https://developer.mozilla.org/en-US/docs/Web/API/console https://developer.mozilla.org/en-US/docs/Web/API/console/timeend https://developer.mozilla.org/en-US/docs/Web/API/console/timelog https://developer.mozilla.org/en-US/docs/Web/API/document/evaluate https://developer.mozilla.org/en-US/docs/Web/API/document/height https://developer.mozilla.org/en-US/docs/Web/API/document/implementation https://developer.mozilla.org/en-US/docs/Web/API/document/lastmodified https://developer.mozilla.org/en-US/docs/Web/API/document/querycommandstate https://developer.mozilla.org/en-US/docs/Web/API/document_object_model/introduction https://developer.mozilla.org/en-US/docs/Web/API/domexception/domexception https://developer.mozilla.org/en-US/docs/Web/API/domimplementation/createdocument https://developer.mozilla.org/en-US/docs/Web/API/domimplementation/createdocumenttype https://developer.mozilla.org/en-US/docs/Web/API/element/getattributenode https://developer.mozilla.org/en-US/docs/Web/API/element/innerhtml https://developer.mozilla.org/en-US/docs/Web/API/element/onfullscreenchange https://developer.mozilla.org/en-US/docs/Web/API/element/requestfullscreen https://developer.mozilla.org/en-US/docs/Web/API/element/setattributenodens https://developer.mozilla.org/en-US/docs/Web/API/eventlistener https://developer.mozilla.org/en-US/docs/Web/API/eventtarget/addeventlistener https://developer.mozilla.org/en-US/docs/Web/API/file/name https://developer.mozilla.org/en-US/docs/Web/API/file/type https://developer.mozilla.org/en-US/docs/Web/API/file/using_files_from_web_applications https://developer.mozilla.org/en-US/docs/Web/API/filereader/onerror https://developer.mozilla.org/en-US/docs/Web/API/geolocation_api/using_the_geolocation_api https://developer.mozilla.org/en-US/docs/Web/API/globaleventhandlers/onerror https://developer.mozilla.org/en-US/docs/Web/API/globaleventhandlers/onkeypress https://developer.mozilla.org/en-US/docs/Web/API/history_api/example https://developer.mozilla.org/en-US/docs/Web/API/html_sanitizer_api https://developer.mozilla.org/en-US/docs/Web/API/htmlanchorelement/rel https://developer.mozilla.org/en-US/docs/Web/API/htmlareaelement/rel https://developer.mozilla.org/en-US/docs/Web/API/htmlcanvaselement/mozfetchasstream https://developer.mozilla.org/en-US/docs/Web/API/htmlimageelement https://developer.mozilla.org/en-US/docs/Web/API/htmllinkelement/rel https://developer.mozilla.org/en-US/docs/Web/API/htmlscriptelement https://developer.mozilla.org/en-US/docs/Web/API/idbobjectstore/getkey https://developer.mozilla.org/en-US/docs/Web/API/indexeddb_api/checking_when_a_deadline_is_due https://developer.mozilla.org/en-US/docs/Web/API/keyboard/getlayoutmap https://developer.mozilla.org/en-US/docs/Web/API/keyboard https://developer.mozilla.org/en-US/docs/Web/API/keyboard_api https://developer.mozilla.org/en-US/docs/Web/API/keyboardevent/altkey https://developer.mozilla.org/en-US/docs/Web/API/keyboardevent/ctrlkey https://developer.mozilla.org/en-US/docs/Web/API/keyboardevent https://developer.mozilla.org/en-US/docs/Web/API/keyboardevent/metakey https://developer.mozilla.org/en-US/docs/Web/API/keyboardevent/shiftkey https://developer.mozilla.org/en-US/docs/Web/API/keyboardlayoutmap/get https://developer.mozilla.org/en-US/docs/Web/API/keyboardlayoutmap https://developer.mozilla.org/en-US/docs/Web/API/mediastreamevent https://developer.mozilla.org/en-US/docs/Web/API/mediastreamevent/stream https://developer.mozilla.org/en-US/docs/Web/API/mediastreamtrack/onoverconstrained https://developer.mozilla.org/en-US/docs/Web/API/microdata_dom_api https://developer.mozilla.org/en-US/docs/Web/API/mouseevent https://developer.mozilla.org/en-US/docs/Web/API/navigator/appversion https://developer.mozilla.org/en-US/docs/Web/API/navigator/oscpu https://developer.mozilla.org/en-US/docs/Web/API/nodefilter/acceptnode https://developer.mozilla.org/en-US/docs/Web/API/nodefilter https://developer.mozilla.org/en-US/docs/Web/API/notification https://developer.mozilla.org/en-US/docs/Web/API/notification/requestpermission https://developer.mozilla.org/en-US/docs/Web/API/notifications_api/using_the_notifications_api https://developer.mozilla.org/en-US/docs/Web/API/pagetransitionevent https://developer.mozilla.org/en-US/docs/Web/API/pointer_lock_api https://developer.mozilla.org/en-US/docs/Web/API/rtcpeerconnection/currentlocaldescription https://developer.mozilla.org/en-US/docs/Web/API/rtcpeerconnection/currentremotedescription https://developer.mozilla.org/en-US/docs/Web/API/rtcpeerconnection/localdescription https://developer.mozilla.org/en-US/docs/Web/API/rtcpeerconnection/onremovestream https://developer.mozilla.org/en-US/docs/Web/API/rtcpeerconnectioniceevent/candidate https://developer.mozilla.org/en-US/docs/Web/API/rtcpeerconnectioniceevent https://developer.mozilla.org/en-US/docs/Web/API/rtcsessiondescription/sdp https://developer.mozilla.org/en-US/docs/Web/API/rtcsessiondescription/tojson https://developer.mozilla.org/en-US/docs/Web/API/rtcsessiondescription/type https://developer.mozilla.org/en-US/docs/Web/API/sanitizer https://developer.mozilla.org/en-US/docs/Web/API/sanitizer/sanitize https://developer.mozilla.org/en-US/docs/Web/API/sanitizer/sanitizer https://developer.mozilla.org/en-US/docs/Web/API/sanitizer/sanitizetostring https://developer.mozilla.org/en-US/docs/Web/API/selection https://developer.mozilla.org/en-US/docs/Web/API/selection/tostring https://developer.mozilla.org/en-US/docs/Web/API/trusted_types_api https://developer.mozilla.org/en-US/docs/Web/API/trustedhtml https://developer.mozilla.org/en-US/docs/Web/API/trustedhtml/tojson https://developer.mozilla.org/en-US/docs/Web/API/trustedhtml/tostring https://developer.mozilla.org/en-US/docs/Web/API/trustedtypepolicy/createhtml https://developer.mozilla.org/en-US/docs/Web/API/trustedtypepolicy https://developer.mozilla.org/en-US/docs/Web/API/trustedtypepolicyfactory https://developer.mozilla.org/en-US/docs/Web/API/urlsearchparams/set https://developer.mozilla.org/en-US/docs/Web/API/videoplaybackquality/totalframedelay https://developer.mozilla.org/en-US/docs/Web/API/web_audio_api/migrating_from_webkitaudiocontext https://developer.mozilla.org/en-US/docs/Web/API/web_workers_api/using_web_workers https://developer.mozilla.org/en-US/docs/Web/API/webgl_api/by_example/clearing_by_clicking https://developer.mozilla.org/en-US/docs/Web/API/webgl_api/by_example/simple_color_animation https://developer.mozilla.org/en-US/docs/Web/API/webgl_api/tutorial/adding_2d_content_to_a_webgl_context https://developer.mozilla.org/en-US/docs/Web/API/webgl_api/tutorial/getting_started_with_webgl https://developer.mozilla.org/en-US/docs/Web/API/webrtc_api/signaling_and_video_calling https://developer.mozilla.org/en-US/docs/Web/API/window/alert https://developer.mozilla.org/en-US/docs/Web/API/window/find https://developer.mozilla.org/en-US/docs/Web/API/window/location https://developer.mozilla.org/en-US/docs/Web/API/window/navigator https://developer.mozilla.org/en-US/docs/Web/API/window/prompt https://developer.mozilla.org/en-US/docs/Web/API/windoworworkerglobalscope/cleartimeout https://developer.mozilla.org/en-US/docs/Web/API/workernavigator/appversion https://developer.mozilla.org/en-US/docs/Web/API/xmldocument/load https://developer.mozilla.org/en-US/docs/Web/API/xmlhttprequest/how_to_check_the_secruity_state_of_an_xmlhttprequest_over_ssl

We might want to replace these as well, but they are not actually broken unless they're in a live sample.

advitiya7 commented 1 year ago

hey can you help me understand the problem i would like to contribute to this thanks

teoli2003 commented 1 year ago

On two of the remaining pages, there are live example using alert(), like this one: https://developer.mozilla.org/en-US/docs/Web/API/eventtarget/addeventlistener#example_of_options_usage

Such example are bad practice (we should not use alert() and are not working on some browsers.

The goal is to replace them using a log element that we use to display the alert text instead.

The numerous merged PRs show many examples of such conversion.

advitiya7 commented 1 year ago

thanks for the help @teoli2003 so basically i have to replace alert() with console.log right? and for clear alert() i should do clear message() sorry if this seems silly to you it's my first time and i want it to be right.

teoli2003 commented 1 year ago

No, it is more complex than that. You need to add an element, like <pre id="log">, and write inside it (using textContent) instead of using alert(): Usingconsole.log()` is not enough as the log will be hidden by default.

For clearing the message, you simply set a new value with textContent.

Of course, positioning the message box may be tricky, and it is likely you'll need to expand the size in {{EmbedLiveSample}} so that no scrollbar appears.

evelinabe commented 5 months ago

I took a look at this one MDN/Writing_guidelines/Page_structures/Live_samples Since the intention of the section is to show a simple example of a JavaScript function, I get the feeling that an alternative to alert() will be too complex. I was thinking about replacing the alert with manipulating the text inside the div on click instead, what would you think of something like this?

It's a bit more complex than the original, but still a simple interaction that keeps most elements in the example as is.

const el = document.getElementById("item")

let toggleClick = false

el.onclick = function () {
  if (!toggleClick) {
    this.textContent = "Owww, stop poking me!"
    toggleClick = true
  } else {
    this.textContent = "Hello world! Welcome to MDN"
    toggleClick = false
  }
}
Josh-Cena commented 5 months ago

Yeah, something like that is what we are looking for.

hamishwillee commented 5 months ago

The https://developer.mozilla.org/en-US/docs/MDN/Writing_guidelines/Page_structures/Live_samples#displaying_a_log_that_appends_items has two examples of how you can create a log to use instead of an alert - one that displays a single "current value" and the other than appends results. This can replace an alert of console.log() example in most cases

Josh-Cena commented 5 months ago

I just realized we have some prompt too, so I'm adding them to this list:

evelinabe commented 4 months ago

The first six files in the list belong to the same tutorial, so they should have the same solution. The alert is used to display the message "Game Over". Would you prefer the simple solution of adding a log below the button with the message or expanding Games/Tutorials/2D_Breakout_game_pure_JavaScript/Game_over somewhat with a gameOverfunction that shows the message on an overlay on the game div for a couple of seconds before reload?

Josh-Cena commented 4 months ago

I'm not very sure. They were brought into live examples by @bsmth, so perhaps he has some ideas here? I think using a <dialog> is the right approach.

bsmth commented 4 months ago

That was me in https://github.com/mdn/content/pull/32258 which fixed var etc., but didn't catch alert() uses. Good idea about <dialog>, I think we can use that instead.