SHARE
Facebook X Pinterest WhatsApp

Creating Dynamic Websites Using JavaScript

Oct 7, 2008

In the last article we discussed a method for creating a modular Javascript toolbox. Having this ability is a great boon for rapid development, but does little in the way of being very useful on its own–so this week we’ll move on to one of the most common uses for Javascript since its debut: modifying the appearance of your website!

Thanks to standards developed by the W3C, as well as concepts based in SGML, each page has a document heirerarchy associated with it. We call this the DOM, (Document Object Model), and it is very useful for creating DHTML (Dynamic HTML) pages when used in conjunction with CSS and Javascript. There are books that cover the concept of the DOM by itself so it is slightly outside of the scope of this article, however we can use it to create our own dynamic pages.

Let’s start by creating some generic CSS classes that will allow us to manipulate the page via JavaScript.

[CODE]
dynamic_style.css
/*
Any styles applied here will affect the BODY tag
in your page, a good place to put background-color
or background-image definitions instead of the HTML
code, to better separate layout from content!
*/
body{
background-color: #FFFFFF;
}
.dBackgroundBox{
background-color: #FFFFFF;
}
.dColorBox{
color: #000000;
}
.dSizeBox{
width: 400px;
height: 600px;
}
.dPositionBox{
position: absolute;
left: 15px;
top: 15px;
}
[/CODE]

Note that while I have separated these classes based on their demonstration function (background color, text color, size, position) you could modify ANY attribute of the CSS as long as it is declared specifically in the CSS file. Note that if you do not declare a style for a class or tag, some JavaScript implementations will refuse to modify them; this can be a hassle in debugging.


Now let’s create a JavaScript file that we can use to modify these display elements:

[CODE]
dynamic_display.js
function ModifyBGColor(id, newColor)
{
 var mElement = document.getElementById(id);
 mElement.style.backgroundColor = newColor;
}
function ModifyTextColor(id, newColor)
{
 var mElement = document.getElementById(id);
 mElement.style.color = newColor;
}
function ModifyBoxSize(id, newWidth, newHeight)
{
 var mElement = document.getElementById(id);
 mElement.style.width = newWidth;
 mElement.style.height = newheight;
}
function ModifyBoxPosition(id, newLeft, newTop)
{
 var mElement = document.getElementById(id);
 mElement.style.left = newLeft;
 mElement.style.top = newTop;
}
[/CODE]

And now to see them in action (remember to properly include the file from the

last article

!):


[CODE]
ourTestPage.html
<HTML>
<HEAD>
<TITLE>Our Display Test</TITLE>
<script src=”modules_control.js” type=”text/javascript”>
</script>
<script type=”text/javascript”>
AddModule(“dynamic_display.js”);
</script>
</HEAD>
<BODY>
<div class=”dBackgroundBox” id=”bgModBox”>
<p><a onclick=”ModifyBGColor(‘bgModBox’,’#FF0000′);”>Click here to modify the background color!</a></p>
</div>
<div class=”dColorBox” id=”cModBox”>
<p><a onclick=”ModifyTextColor(‘cModBox’,’#00FF00′);”>Click here to modify the text color!</a></p>
</div>
<div class=”dSizeBox” id=”szModBox”>
<p><a onclick=”ModifyBoxSize(‘szModBox’,’900px’,100px’);”>Click here to modify the box size!</a></p>
</div>
<div class=”dPositionBox” id=”pModBox”>
<p><a onclick=”ModifyBoxPosition(‘pModBox’,’100px’,100px’);”>Click here to modify the box position!</a></p>
</div>
</BODY>
</HTML>
[/CODE]

Please note:



  • The quotations marks are singular, not double, in the onclick references!
  • The size and position parameters are strings followed by either “px” or “%”, such as “10%” or “100px”, just as in the CSS declarations!
  • Elements can be associated with more than one type of class and custom classes can be created with other styles declared; as long as the style is declared you can manipulate them with these functions and the ID of the element!

Now we have working dynamic display code that we can use to change the appearance of our web sites on the fly, letting your visitors use their own preferences!

Recommended for you...

The Revolutionary ES6 Rest and Spread Operators
Rob Gravelle
Aug 23, 2022
Ahead of Time (AOT) Compilation in Angular
Tariq Siddiqui
Aug 16, 2022
Converting a JavaScript Object to a String
Rob Gravelle
Aug 14, 2022
Understanding Primitive Type Coercion in JavaScript
Rob Gravelle
Jul 28, 2022
HTML Goodies Logo

The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.