diff --git a/files/en-us/learn/server-side/django/web_application_security/index.md b/files/en-us/learn/server-side/django/web_application_security/index.md index c62d24cf83a43ed..1aecef5251752ca 100644 --- a/files/en-us/learn/server-side/django/web_application_security/index.md +++ b/files/en-us/learn/server-side/django/web_application_security/index.md @@ -57,19 +57,22 @@ Django's template system protects you against the majority of XSS attacks by [es 2. Open the site in your local browser and login to your superuser account. 3. Navigate to the author-creation page (which should be at URL: `http://127.0.0.1:8000/catalog/author/create/`). 4. Enter names and date details for a new user, and then append the following text to the Last Name field: - ``. - ![Author Form XSS test](author_create_form_alert_xss.png) + ``. + ![Author Form XSS test](author_create_form_alert_xss.png) - > **Note:** This is a harmless script that, if executed, will display an alert box in your browser. If the alert is displayed when you submit the record then the site is vulnerable to XSS threats. + > **Note:** This is a harmless script that, if executed, will display an alert box in your browser. If the alert is displayed when you submit the record then the site is vulnerable to XSS threats. 5. Press **Submit** to save the record. 6. When you save the author it will be displayed as shown below. Because of the XSS protections the `alert()` should not be run. Instead the script is displayed as plain text. - ![Author detail view XSS test](author_detail_alert_xss.png) + ![Author detail view XSS test](author_detail_alert_xss.png) If you view the page HTML source code, you can see that the dangerous characters for the script tags have been turned into their harmless escape code equivalents (for example, `>` is now `>`) ```html -

Author: Boon<script>alert('Test alert');</script>, David (Boonie)

+

+ Author: Boon<script>alert('Test alert');</script>, David + (Boonie) +

``` Using Django templates protects you against the majority of XSS attacks. However it is possible to turn off this protection, and the protection isn't automatically applied to all tags that wouldn't normally be populated by user input (for example, the `help_text` in a form field is usually not user-supplied, so Django doesn't escape those values). @@ -86,20 +89,38 @@ In order to do this, they might create an HTML file like the one below, which co They would then send the file to all the Librarians and suggest that they open the file (it contains some harmless information, honest!). If the file is opened by any logged in librarian, then the form would be submitted with their credentials and a new author would be created. ```html - - - -
- - - - - -
- -
- - + + +
+ + + + + + + + + + + + + + + + + +
+ +
+ +
+ +
+ +
+ +
+ ``` @@ -108,7 +129,7 @@ Run the development web server, and log in with your superuser account. Copy the The way the protection is enabled is that you include the `{% csrf_token %}` template tag in your form definition. This token is then rendered in your HTML as shown below, with a value that is specific to the user on the current browser. ```html - + ``` Django generates a user/browser specific key and will reject forms that do not contain the field, or that contain an incorrect field value for the user/browser. diff --git a/files/en-us/mozilla/add-ons/webextensions/add_a_button_to_the_toolbar/index.md b/files/en-us/mozilla/add-ons/webextensions/add_a_button_to_the_toolbar/index.md index ceac81dab78025e..879bdf523613a0e 100644 --- a/files/en-us/mozilla/add-ons/webextensions/add_a_button_to_the_toolbar/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/add_a_button_to_the_toolbar/index.md @@ -24,7 +24,6 @@ First, create a new directory, "button", and create a file called "manifest.json ```json { - "description": "Demonstrating toolbar buttons", "manifest_version": 2, "name": "button-demo", @@ -40,7 +39,6 @@ First, create a new directory, "button", and create a file called "manifest.json "32": "icons/page-32.png" } } - } ``` @@ -95,7 +93,6 @@ Let's try adding a popup to the button. Replace manifest.json with this: ```json { - "description": "Demonstrating toolbar buttons", "manifest_version": 2, "name": "button-demo", @@ -109,7 +106,6 @@ Let's try adding a popup to the button. Replace manifest.json with this: "32": "icons/page-32.png" } } - } ``` @@ -123,20 +119,17 @@ So now we need to create that popup. Create a directory called "popup" then crea ```html - - + - - -
developer.mozilla.org
-
support.mozilla.org
-
addons.mozilla.org
- - - + +
developer.mozilla.org
+
support.mozilla.org
+
addons.mozilla.org
+ + ``` @@ -145,7 +138,8 @@ You can see that this is a normal HTML page containing three {{htmlelement("div" Create a file called "choose_page.css" inside the "popup" directory, and give it these contents: ```css -html, body { +html, +body { width: 300px; } diff --git a/files/en-us/mozilla/add-ons/webextensions/content_security_policy/index.md b/files/en-us/mozilla/add-ons/webextensions/content_security_policy/index.md index 36a3ec62b4ff60c..f2717c62cd25892 100644 --- a/files/en-us/mozilla/add-ons/webextensions/content_security_policy/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/content_security_policy/index.md @@ -14,16 +14,12 @@ Like websites, extensions can load content from different sources. For example, ```html - - + - - - - ``` @@ -66,7 +61,7 @@ These policies are applied to any extension that has not explicitly set its own Under the default CSP you may only load [\ + ``` This doesn't load the requested resource: it fails silently, and any object that you expect to be present from the resource is not found. There are two main solutions to this: @@ -95,7 +90,9 @@ const f = new Function("console.log('foo');"); Under the default CSP, inline JavaScript is not executed. This disallows both JavaScript placed directly in ` + ``` ```html diff --git a/files/en-us/mozilla/add-ons/webextensions/implement_a_settings_page/index.md b/files/en-us/mozilla/add-ons/webextensions/implement_a_settings_page/index.md index 379802aa90c1b71..a94828dce16ee7e 100644 --- a/files/en-us/mozilla/add-ons/webextensions/implement_a_settings_page/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/implement_a_settings_page/index.md @@ -28,7 +28,6 @@ Create a new directory called `settings`, then create a file called `manifest.js ```json { - "manifest_version": 2, "name": "Settings example", "version": "1.0", @@ -39,7 +38,6 @@ Create a new directory called `settings`, then create a file called `manifest.js "js": ["borderify.js"] } ] - } ``` @@ -63,7 +61,6 @@ First, update `manifest.json` so it has these contents: ```json { - "manifest_version": 2, "name": "Settings example", "version": "1.0", @@ -86,7 +83,6 @@ First, update `manifest.json` so it has these contents: "id": "addon@example.com" } } - } ``` @@ -103,23 +99,19 @@ Next, because we've promised to provide `options.html`, let's create it. Create ```html - - + -
- - + +
- - ``` @@ -136,7 +128,6 @@ function saveOptions(e) { } function restoreOptions() { - function setCurrentChoice(result) { document.querySelector("#color").value = result.color || "blue"; } diff --git a/files/en-us/mozilla/add-ons/webextensions/user_interface/options_pages/index.md b/files/en-us/mozilla/add-ons/webextensions/user_interface/options_pages/index.md index 1b42034dc11b4e4..1708238fa9a6f99 100644 --- a/files/en-us/mozilla/add-ons/webextensions/user_interface/options_pages/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/user_interface/options_pages/index.md @@ -23,7 +23,7 @@ To create an options page, write an HTML file defining the page. This page can i ```html - + diff --git a/files/en-us/mozilla/add-ons/webextensions/working_with_the_tabs_api/index.md b/files/en-us/mozilla/add-ons/webextensions/working_with_the_tabs_api/index.md index cdd80338e0978ca..bb97c00f4c43ce8 100644 --- a/files/en-us/mozilla/add-ons/webextensions/working_with_the_tabs_api/index.md +++ b/files/en-us/mozilla/add-ons/webextensions/working_with_the_tabs_api/index.md @@ -95,7 +95,7 @@ To see how {{WebExtAPIRef("tabs.query()")}} and {{WebExtAPIRef("tabs.Tab")}} are ```html - + diff --git a/files/en-us/web/accessibility/seizure_disorders/index.md b/files/en-us/web/accessibility/seizure_disorders/index.md index c5df8bf4bc09caf..d4b5c3928a6411a 100644 --- a/files/en-us/web/accessibility/seizure_disorders/index.md +++ b/files/en-us/web/accessibility/seizure_disorders/index.md @@ -196,7 +196,7 @@ Consider using metadata such as ` + @@ -266,8 +266,10 @@ To ensure that a video has controls that a user can access, ensure that you add ``` @@ -332,11 +334,9 @@ JavaScript is often used to control {{HTMLElement('canvas')}} elements and SVGs. [document.getAnimations()](/en-US/docs/Web/API/Document/getAnimations) is an experimental technology, and includes [CSS Animations](/en-US/docs/Web/CSS/CSS_Animations), [CSS Transitions](/en-US/docs/Web/CSS/CSS_Transitions), and [Web Animations](/en-US/docs/Web/API/Web_Animations_API). The MDN page on [Document.getAnimations()](/en-US/docs/Web/API/Document/getAnimations) provides the following code sample of how to slow down all animations on a page to half speed: ```js -document.getAnimations().forEach( - function (animation) { - animation.playbackRate *= .5; - } -); +document.getAnimations().forEach((animation) => { + animation.playbackRate *= 0.5; +}); ``` **Image sources for animation** @@ -490,7 +490,7 @@ This can be useful if the ambient light API is not available. Support is emergin ```css @media (prefers-color-scheme: dark) { - /* adjust styles for dark mode */ + /* adjust styles for dark mode */ } ``` @@ -511,10 +511,11 @@ Eric Bailey, of CSS-Tricks, found an innovative use the update feature which, us ```css @media screen and (prefers-reduced-motion: reduce), (update: slow) { - * { animation-duration: 0.001ms !important; - animation-iteration-count: 1 !important; /* Hat tip Nick/cssremedy (https://css-tricks.com/revisiting-prefers-reduced-motion/#comment-1700170) */ - transition-duration: 0.001ms !important; - } + * { + animation-duration: 0.001ms !important; + animation-iteration-count: 1 !important; /* Hat tip Nick/cssremedy (https://css-tricks.com/revisiting-prefers-reduced-motion/#comment-1700170) */ + transition-duration: 0.001ms !important; + } } ``` diff --git a/files/en-us/web/api/canvas_api/tutorial/drawing_shapes/index.md b/files/en-us/web/api/canvas_api/tutorial/drawing_shapes/index.md index 6885ae832e27e5b..20ddf32d00ad8a6 100644 --- a/files/en-us/web/api/canvas_api/tutorial/drawing_shapes/index.md +++ b/files/en-us/web/api/canvas_api/tutorial/drawing_shapes/index.md @@ -43,7 +43,7 @@ Below is the `draw()` function from the previous page, but now it is making use ### Rectangular shape example ```html hidden - + @@ -109,7 +109,7 @@ The third, and an optional step, is to call `closePath()`. This method tries to For example, the code for drawing a triangle would look something like this: ```html hidden - + @@ -147,27 +147,27 @@ When the canvas is initialized or `beginPath()` is called, you typically will wa To try this for yourself, you can use the code snippet below. Just paste it into the `draw()` function we saw earlier. ```html hidden - - - - + + + + ``` ```js function draw() { - const canvas = document.getElementById('canvas'); + const canvas = document.getElementById("canvas"); if (canvas.getContext) { - const ctx = canvas.getContext('2d'); + const ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.arc(75, 75, 50, 0, Math.PI * 2, true); // Outer circle ctx.moveTo(110, 75); - ctx.arc(75, 75, 35, 0, Math.PI, false); // Mouth (clockwise) + ctx.arc(75, 75, 35, 0, Math.PI, false); // Mouth (clockwise) ctx.moveTo(65, 65); - ctx.arc(60, 65, 5, 0, Math.PI * 2, true); // Left eye + ctx.arc(60, 65, 5, 0, Math.PI * 2, true); // Left eye ctx.moveTo(95, 65); - ctx.arc(90, 65, 5, 0, Math.PI * 2, true); // Right eye + ctx.arc(90, 65, 5, 0, Math.PI * 2, true); // Right eye ctx.stroke(); } } @@ -193,7 +193,7 @@ This method takes two arguments, `x` and `y`, which are the coordinates of the l The example below draws two triangles, one filled and one outlined. ```html hidden - + @@ -254,7 +254,7 @@ The statement for the `clockwise` parameter results in the first and third row b > **Note:** This example requires a slightly larger canvas than the others on this page: 150 x 200 pixels. ```html hidden - + @@ -315,10 +315,10 @@ There's nothing very difficult in these examples. In both cases we see a success This example uses multiple quadratic Bézier curves to render a speech balloon. ```html hidden - - - - + + + + ``` @@ -349,10 +349,10 @@ function draw() { This example draws a heart using cubic Bézier curves. ```html hidden - - - - + + + + ``` @@ -392,7 +392,7 @@ Before this method is executed, the `moveTo()` method is automatically called wi So far, each example on this page has used only one type of path function per shape. However, there's no limitation to the number or types of paths you can use to create a shape. So in this final example, let's combine all of the path functions to make a set of very famous game characters. ```html hidden - + @@ -515,10 +515,10 @@ The `Path2D` API also adds a way to combine paths using the `addPath` method. Th In this example, we are creating a rectangle and a circle. Both are stored as a `Path2D` object, so that they are available for later usage. With the new `Path2D` API, several methods got updated to optionally accept a `Path2D` object to use instead of the current path. Here, `stroke` and `fill` are used with a path argument to draw both objects onto the canvas, for example. ```html hidden - - - - + + + + ``` diff --git a/files/en-us/web/api/canvas_api/tutorial/using_images/index.md b/files/en-us/web/api/canvas_api/tutorial/using_images/index.md index eb3b959b18bdeb9..f5301b33edaea40 100644 --- a/files/en-us/web/api/canvas_api/tutorial/using_images/index.md +++ b/files/en-us/web/api/canvas_api/tutorial/using_images/index.md @@ -126,7 +126,7 @@ Once we have a reference to our source image object we can use the `drawImage()` In the following example, we will use an external image as the backdrop for a small line graph. Using backdrops can make your script considerably smaller because we can avoid the need for code to generate the background. In this example, we're only using one image, so I use the image object's `load` event handler to execute the drawing statements. The `drawImage()` method places the backdrop at the coordinate (0, 0), which is the top-left corner of the canvas. ```html hidden - + @@ -168,7 +168,7 @@ In this example, we'll use an image as a wallpaper and repeat it several times o > **Note:** Images can become blurry when scaling up or grainy if they're scaled down too much. Scaling is probably best not done if you've got some text in it which needs to remain legible. ```html hidden - + @@ -214,7 +214,7 @@ Slicing can be a useful tool when you want to make compositions. You could have In this example, we'll use the same rhino as in the previous example, but we'll slice out its head and composite it into a picture frame. The picture frame image is a 24-bit PNG which includes a drop shadow. Because 24-bit PNG images include a full 8-bit alpha channel, unlike GIF and 8-bit PNG images, it can be placed onto any background without worrying about a matte color. ```html - +
@@ -254,7 +254,7 @@ In this case, every image has a fixed width and height, as does the frame that's The code below should be self-explanatory. We loop through the {{domxref("document.images")}} container and add new canvas elements accordingly. Probably the only thing to note, for those not so familiar with the DOM, is the use of the {{domxref("Node.insertBefore")}} method. `insertBefore()` is a method of the parent node (a table cell) of the element (the image) before which we want to insert our new node (the canvas element). ```html - + @@ -269,8 +269,8 @@ The code below should be self-explanatory. We loop through the {{domxref("docume -
- + + ``` @@ -300,28 +300,25 @@ Tying it all together is the JavaScript to draw our framed images: ```js function draw() { - // Loop through all images for (let i = 0; i < document.images.length; i++) { - // Don't add a canvas for the frame image - if (document.images[i].getAttribute('id') !== 'frame') { - + if (document.images[i].getAttribute("id") !== "frame") { // Create canvas element - canvas = document.createElement('canvas'); - canvas.setAttribute('width', 132); - canvas.setAttribute('height', 150); + canvas = document.createElement("canvas"); + canvas.setAttribute("width", 132); + canvas.setAttribute("height", 150); // Insert before the image - document.images[i].parentNode.insertBefore(canvas,document.images[i]); + document.images[i].parentNode.insertBefore(canvas, document.images[i]); - ctx = canvas.getContext('2d'); + ctx = canvas.getContext("2d"); // Draw image to canvas ctx.drawImage(document.images[i], 15, 20); // Add frame - ctx.drawImage(document.getElementById('frame'), 0, 0); + ctx.drawImage(document.getElementById("frame"), 0, 0); } } } diff --git a/files/en-us/web/api/css_object_model/using_dynamic_styling_information/index.md b/files/en-us/web/api/css_object_model/using_dynamic_styling_information/index.md index 821ba8e2eb23049..10f26dd410c6786 100644 --- a/files/en-us/web/api/css_object_model/using_dynamic_styling_information/index.md +++ b/files/en-us/web/api/css_object_model/using_dynamic_styling_information/index.md @@ -24,7 +24,7 @@ To get to the `style` objects from the `document`, you can use the {{domxref("Do In this example the background of the page is set to red using CSS. The JavaScript then accesses the property using the CSSOM and changes the background to blue. ```html - + Modifying a stylesheet rule with CSSOM @@ -87,7 +87,7 @@ To change a particular element's style, you can adapt the following example for

- Click here to change background color. + Click here to change background color.

@@ -124,13 +124,13 @@ More important than the two properties noted here is the use of the `style` obje document.getElementById('d').style.color = 'black'; } - + - -
Thunder
- - - + +
Thunder
+ + + ``` diff --git a/files/en-us/web/api/document/getelementbyid/index.md b/files/en-us/web/api/document/getelementbyid/index.md index e7e264d69bb12ce..0207a805df05c72 100644 --- a/files/en-us/web/api/document/getelementbyid/index.md +++ b/files/en-us/web/api/document/getelementbyid/index.md @@ -40,15 +40,15 @@ An {{domxref("Element")}} object describing the DOM element object matching the ### HTML ```html - - - getElementById example - - -

Some text here

- - - + + + getElementById example + + +

Some text here

+ + + ``` @@ -76,24 +76,24 @@ Unlike some other element-lookup methods such as {{domxref("Document.querySelect ```html - + Document - - + +
-

hello word1

-

hello word2

-

hello word3

-

hello word4

+

hello word1

+

hello word2

+

hello word3

+

hello word4

- + ``` diff --git a/files/en-us/web/api/document/getelementsbyclassname/index.md b/files/en-us/web/api/document/getelementsbyclassname/index.md index 1a2e7aa288d1a90..eceff70338b05b3 100644 --- a/files/en-us/web/api/document/getelementsbyclassname/index.md +++ b/files/en-us/web/api/document/getelementsbyclassname/index.md @@ -89,25 +89,25 @@ const testDivs = Array.prototype.filter.call( This is the most commonly used method of operation. ```html - - + +
-

hello world 1

-

hello world 2

-

hello world 3

-

hello world 4

+

hello world 1

+

hello world 2

+

hello world 3

+

hello world 4

- + ``` @@ -133,15 +133,15 @@ elements with ALL of the classNames specified are selected. // getElementsByClassName only selects elements that have both given classes const allOrangeJuiceByClass = document.getElementsByClassName('orange juice'); let result = "document.getElementsByClassName('orange juice')"; -for (let i=0; i < allOrangeJuiceByClass.length; i++) { - result += `\n ${allOrangeJuiceByClass[i].textContent}`; +for (let i = 0; i < allOrangeJuiceByClass.length; i++) { + result += `\n ${allOrangeJuiceByClass[i].textContent}`; } // querySelector only selects full complete matches const allOrangeJuiceQuery = document.querySelectorAll('.orange.juice'); result += "\n\ndocument.querySelectorAll('.orange.juice')"; -for (let i=0; i < allOrangeJuiceQuery.length; i++) { - result += `\n ${allOrangeJuiceQuery[i].textContent}`; +for (let i = 0; i < allOrangeJuiceQuery.length; i++) { + result += `\n ${allOrangeJuiceQuery[i].textContent}`; } document.getElementById("resultArea").value = result; diff --git a/files/en-us/web/api/document/getelementsbytagnamens/index.md b/files/en-us/web/api/document/getelementsbytagnamens/index.md index 0a95acbbfcf545b..adfdd3f3aebb974 100644 --- a/files/en-us/web/api/document/getelementsbytagnamens/index.md +++ b/files/en-us/web/api/document/getelementsbytagnamens/index.md @@ -36,7 +36,7 @@ getElementsByTagNameNS(namespace, name) ### Return value A live {{DOMxRef("NodeList")}} (but see the note below) of - found elements in the order they appear in the tree. +found elements in the order they appear in the tree. > **Note:** While the W3C specification says returned value is a `NodeList`, this method returns a {{DOMxRef("HTMLCollection")}} both in Gecko and Internet Explorer. > Opera returns a `NodeList`, but with a `namedItem` method implemented, which makes it similar to a `HTMLCollection`. As of January 2012, only in WebKit browsers is the returned value a pure `NodeList`. @@ -59,73 +59,72 @@ To use the following example, just copy/paste it into a new file saved with the extension. ```html - - - -getElementsByTagNameNS example - - - - - -

Some outer text

-

Some outer text

- -
-

Some div1 text

-

Some div1 text

-

Some div1 text

- -
-

Some div2 text

-

Some div2 text

+ + + getElementsByTagNameNS example + + + + + +

Some outer text

+

Some outer text

+ +
+

Some div1 text

+

Some div1 text

+

Some div1 text

+ +
+

Some div2 text

+

Some div2 text

+
-
- -

Some outer text

-

Some outer text

-
+

Some outer text

+

Some outer text

-
+
- +
- + + ``` diff --git a/files/en-us/web/api/document/mscapslockwarningoff/index.md b/files/en-us/web/api/document/mscapslockwarningoff/index.md index a9a394c1d7a9039..68eca0b57445432 100644 --- a/files/en-us/web/api/document/mscapslockwarningoff/index.md +++ b/files/en-us/web/api/document/mscapslockwarningoff/index.md @@ -37,24 +37,28 @@ Fiddle: ## Example 2 ```html - - - msCapsLockWarningOff example - - - -
- - + + +
+ +
+
+ +
+ ``` diff --git a/files/en-us/web/api/document/write/index.md b/files/en-us/web/api/document/write/index.md index f581509c924de02..ddb4ff9e492c06d 100644 --- a/files/en-us/web/api/document/write/index.md +++ b/files/en-us/web/api/document/write/index.md @@ -41,24 +41,22 @@ None ({{jsxref("undefined")}}). ## Examples ```html - - - - Write example - - - - - -

Some original document content.

- - + + + Write example + + + + + +

Some original document content.

+ ``` @@ -74,7 +72,7 @@ If the `document.write()` call is embedded within an inline HTML ` ``` diff --git a/files/en-us/web/api/document_object_model/introduction/index.md b/files/en-us/web/api/document_object_model/introduction/index.md index 894f4726102d0df..5fa1feb4b1bf3f3 100644 --- a/files/en-us/web/api/document_object_model/introduction/index.md +++ b/files/en-us/web/api/document_object_model/introduction/index.md @@ -92,22 +92,20 @@ adds text to that element, and then adds it to the tree for the document: ```html - + - - + ``` @@ -134,9 +132,7 @@ The following table briefly describes these data types. ownerDocument property of an element returns the document to which it belongs), this object is the root document object itself. The - DOM document Reference + DOM document Reference chapter describes the document object. @@ -226,8 +222,9 @@ const table = document.getElementById("table"); const tableAttrs = table.attributes; // Node/Element interface for (let i = 0; i < tableAttrs.length; i++) { // HTMLTableElement interface: border attribute - if (tableAttrs[i].nodeName.toLowerCase() === "border") + if (tableAttrs[i].nodeName.toLowerCase() === "border") { table.border = "1"; + } } // HTMLTableElement interface: summary attribute table.summary = "note: increased border"; @@ -263,7 +260,7 @@ The following simple example illustrates using the DOM {{domxref("Document")}} A - the documents's link color (that is, the color of any hypertext links anywhere in the document) ```html - + Simple Document API example - - - + myBody.appendChild(myTable); + // sets the border attribute of myTable to 2; + myTable.setAttribute("border", "2"); + } + + + ``` @@ -322,37 +330,37 @@ The basic steps to create the table in sample1.html are: ### Getting a text node from the table -This example introduces two new DOM attributes. First it uses the `childNodes` attribute to get the list of child nodes of mycel. The `childNodes` list includes all child nodes, regardless of what their name or type is. Like `getElementsByTagName()`, it returns a list of nodes. +This example introduces two new DOM attributes. First it uses the `childNodes` attribute to get the list of child nodes of myCell. The `childNodes` list includes all child nodes, regardless of what their name or type is. Like `getElementsByTagName()`, it returns a list of nodes. The differences are that (a) `getElementsByTagName()` only returns elements of the specified tag name; and (b) `getElementsByTagName()` returns descendants at any level, not just immediate children. -Once you have the returned list, use `[x]` method to retrieve the desired child item. This example stores in myceltext the text node of the second cell in the second row of the table. +Once you have the returned list, use `[x]` method to retrieve the desired child item. This example stores in myCellText the text node of the second cell in the second row of the table. -Then, to display the results in this example, it creates a new text node whose content is the data of `myceltext`, and appends it as a child of the `` element. +Then, to display the results in this example, it creates a new text node whose content is the data of `myCellText`, and appends it as a child of the `` element. > **Note:** If your object is a text node, you can use the data attribute and retrieve the text content of the node. ```js -mybody = document.getElementsByTagName("body")[0]; -mytable = mybody.getElementsByTagName("table")[0]; -mytablebody = mytable.getElementsByTagName("tbody")[0]; -myrow = mytablebody.getElementsByTagName("tr")[1]; -mycel = myrow.getElementsByTagName("td")[1]; - -// first item element of the childNodes list of mycel -myceltext=mycel.childNodes[0]; - -// content of currenttext is the data content of myceltext -currenttext=document.createTextNode(myceltext.data); -mybody.appendChild(currenttext); +myBody = document.getElementsByTagName("body")[0]; +myTable = myBody.getElementsByTagName("table")[0]; +myTableBody = myTable.getElementsByTagName("tbody")[0]; +myRow = myTableBody.getElementsByTagName("tr")[1]; +myCell = myRow.getElementsByTagName("td")[1]; + +// first item element of the childNodes list of myCell +myCellText = myCell.childNodes[0]; + +// content of currentText is the data content of myCellText +currentText = document.createTextNode(myCellText.data); +myBody.appendChild(currentText); ``` ### Getting an attribute value -At the end of sample1 there is a call to `setAttribute` on the `mytable` object. This call was used to set the border property of the table. To retrieve the value of the attribute, use the `getAttribute` method: +At the end of sample1 there is a call to `setAttribute` on the `myTable` object. This call was used to set the border property of the table. To retrieve the value of the attribute, use the `getAttribute` method: ```js -mytable.getAttribute("border"); +myTable.getAttribute("border"); ``` ### Hiding a column by changing style properties @@ -360,35 +368,34 @@ mytable.getAttribute("border"); Once you have the object in your JavaScript variable, you can set `style` properties directly. The following code is a modified version of sample1.html in which each cell of the second column is hidden and each cell of the first column is changed to have a red background. Note that the `style` property was set directly. ```html - - - - + ``` diff --git a/files/en-us/web/api/document_object_model/using_the_w3c_dom_level_1_core/index.md b/files/en-us/web/api/document_object_model/using_the_w3c_dom_level_1_core/index.md index d47733d81c2a471..724cf98cde0fdab 100644 --- a/files/en-us/web/api/document_object_model/using_the_w3c_dom_level_1_core/index.md +++ b/files/en-us/web/api/document_object_model/using_the_w3c_dom_level_1_core/index.md @@ -15,14 +15,14 @@ The W3C's [DOM Level 1 Core](https://www.w3.org/TR/REC-DOM-Level-1/level-one-cor A [DOM](https://www.w3.org/TR/REC-DOM-Level-1/introduction.html) tree is a kind of [tree](https://en.wikipedia.org/wiki/Tree_structure) whose nodes represent an HTML or XML document's contents. Each HTML or XML document has a unique DOM tree representation. For example, the following document ```html - - - My Document - - -

Header

-

Paragraph

- + + + My Document + + +

Header

+

Paragraph

+ ``` @@ -46,9 +46,9 @@ Suppose the author wants to change the header of the above document and write tw ```html - -

Header

-

Paragraph

+ +

Header

+

Paragraph

``` @@ -56,27 +56,27 @@ Suppose the author wants to change the header of the above document and write tw ```js function change() { - // document.getElementsByTagName("H2") returns a NodeList of the

- // elements in the document, and the first is number 0: - - const header = document.getElementsByTagName("H2").item(0); - // the firstChild of the header is a Text node: - header.firstChild.data = "A dynamic document"; - // now the header is "A dynamic document". - - const para = document.getElementsByTagName("P").item(0); - para.firstChild.data = "This is the first paragraph."; - - // create a new Text node for the second paragraph - const newText = document.createTextNode("This is the second paragraph."); - // create a new Element to be the second paragraph - const newElement = document.createElement("P"); - // put the text in the paragraph - newElement.appendChild(newText); - // and put the paragraph on the end of the document by appending it to - // the BODY (which is the parent of para) - para.parentNode.appendChild(newElement); - } + // document.getElementsByTagName("h2") returns a NodeList of the

+ // elements in the document, and the first is number 0: + + const header = document.getElementsByTagName("h2").item(0); + // the firstChild of the header is a Text node: + header.firstChild.data = "A dynamic document"; + // now the header is "A dynamic document". + + const para = document.getElementsByTagName("p").item(0); + para.firstChild.data = "This is the first paragraph."; + + // create a new Text node for the second paragraph + const newText = document.createTextNode("This is the second paragraph."); + // create a new Element to be the second paragraph + const newElement = document.createElement("p"); + // put the text in the paragraph + newElement.appendChild(newText); + // and put the paragraph on the end of the document by appending it to + // the body (which is the parent of para) + para.parentNode.appendChild(newElement); +} ``` {{ EmbedLiveSample('A_simple_example', 800, 300) }} diff --git a/files/en-us/web/api/element/haspointercapture/index.md b/files/en-us/web/api/element/haspointercapture/index.md index 089730e1ea5876e..f5eba79155324bc 100644 --- a/files/en-us/web/api/element/haspointercapture/index.md +++ b/files/en-us/web/api/element/haspointercapture/index.md @@ -38,7 +38,7 @@ capture, `false` if it doesn't. ## Examples ```html - + - - - -
+ + +
- -
- - Fix
-
- - + ondrop="event.stopPropagation(); event.preventDefault(); doDrop(event);"> +
Fix
+
+ ``` This example cancels both the {{domxref("HTMLElement/dragenter_event", "dragenter")}} and `{{domxref("HTMLElement/dragover_event", "dragover")}}` events by calling the {{domxref("Event.preventDefault","preventDefault()")}}. method. This allows a drop to occur on that element. -The `dodrop` event handler is called when dropping an item. It checks the {{domxref("DataTransfer.mozItemCount","mozItemCount")}} property to check how many items have been dropped and iterates over them. For each item, the {{domxref("DataTransfer.mozTypesAt","mozTypesAt()")}} method is called to get the list of types. This list is iterated over to get all of the data associated with the drag. +The `doDrop` event handler is called when dropping an item. It checks the {{domxref("DataTransfer.mozItemCount","mozItemCount")}} property to check how many items have been dropped and iterates over them. For each item, the {{domxref("DataTransfer.mozTypesAt","mozTypesAt()")}} method is called to get the list of types. This list is iterated over to get all of the data associated with the drag. This processing is useful if you wish to examine the data that a drag is holding. Drop an item on the drop target in the example to see what items, formats and data was being dragged.