JavaScript DOM CSS

A picture of a decorated open window to signify JavaScript DOM CSS

Create DOM CSS In JavaScript

To demonstrate, CSS styling with JavaScript, a new window will be opened in a new web browser window or tab.

Multiple windows can be opened and reused assuming that JavaScript is not disabled. JavaScript can be used to create inline styles or a internal or external styles.

JavaScript open() method can take optional parameters for the name, URL and window dimensions. Inline style attributes are inside HTML elements, Internal styles use a style element. External CSS styles use the link element in the head section on an HTML document.

Common Syntax Of DOM CSS In JavaScript

Glossary:

URL

Page to open.

Name

Target attribute or name of window.

Specs

Comma-separated list of items such as dimensions, location and scrollbars.

CSS

Cascading Style Sheets inherit properties and methods from the parent.

Common Methods For DOM CSS

Ojamboshop.com Learning JavaScript DOM CSS
Name Description Example
open() Create a new browser window. window.open();
style.property Changing style of an HTML element. style.colour = “blue”;
style=”property:value” Inline CSS. style=”color:blue;”
property:value Internal CSS. {color:blue;”}
href=”styles.css” External CSS. <link rel=”stylesheet” href=”styles.css”>
Name Description Example

JavaScript DOM CSS Snippet

// OjamboShop.com Learning JavaScript DOM CSS Tutorial

// DOM CSS
let newWindow = window.open("", null, "height=200,width=400,status=yes,toolbar=no,menubar=no,location=no");  

// New Window Content
let p = newWindow.document.createElement("p");
let n = newWindow.document.createTextNode("Be sure to check out ");
p.id = 'content';
p.className = 'note';
p.style.color = 'gray';
p.appendChild(n);
newWindow.document.body.appendChild(p);
let link = newWindow.document.createElement("a");
link.setAttribute('href', 'https://ojamboshop.com');
link.appendChild(document.createTextNode('OjamboShop.com'));
p.appendChild(link);
let span = newWindow.document.createElement("span");
span.innerHTML = ` for the courses and to leave a donation click <a href="https://ojambo.com/donate">here<a>`;
p.appendChild(span);
let linkh = newWindow.document.createElement("link");
linkh.setAttribute('rel', 'stylesheet');
linkh.setAttribute('href', 'styles.css');
newWindow.document.body.appendChild(linkh);
let style = newWindow.document.createElement("style");
style.innerHTML = `
	span {
		color: red;
		font-weight: bold;
	}
`;
newWindow.document.body.appendChild(style);

JavaScript DOM CSS Code
OjamboShop.com Web IDE JavaScript DOM CSS Code

JavaScript DOM CSS Result
OjamboShop.com Web IDE Displaying JavaScript DOM CSS Result


Usage

You can use any IDE or text editor and the web browser to compile and execute JavaScript code. For this tutorial, the OjamboShop.com Learning JavaScript Course Web IDE can used to input and compile JavaScript code for the open window DOM CSS.

Open Source

JavaScript follows the ECMAScript standard and is licensed under the W3C Software License by web browser vendors and runtime environment vendors. This allows commercial use, modification, distribution, and allows making derivatives proprietary.

Live Stream

Every Friday, you can join a live stream and ask questions. Check Ojambo.com for details and instructions.

Learn Programming Courses:

Get the Learning JavaScript Course for your web browser on any device.

OjamboShop.com Learning JavaScript Course
OjamboShop.com Learning JavaScript Interactive Online Course

Learn Programming Books:

Learning Javascript Book is available as Learning JavaScript Paperback or Learning JavaScript Ebook.

OjamboShop.com Learning JavaScript Book Announcement
OjamboShop.com Learning JavaScript Ebook And Paperback Announcement

Conclusion:

JavaScript makes it easy to create CSS styled content for new windows. New windows can be opened in the same window, as a new window or new tab.

If you enjoy this article, consider supporting me by purchasing one of my OjamboShop.com Online Programming Courses or publications at Edward Ojambo Programming Books

References:

Leave a Reply

Your email address will not be published. Required fields are marked *