| < Day Day Up > |
Hack 6. Add or Remove Content on a Page
Use DOM
Since most
1.7.1. Adding an ElementThe following code adds a new element to the end of the page. The element will appear at the bottom of the page, unless you style it with CSS to position it somewhere else [Hack #7] :
var elmNewContent = document.createElement('div');
document.body.appendChild(elmNewContent)
1.7.2. Removing an Element
You can also remove elements from a page. Removed elements disappear from the page (obviously), and any content after them
collapses
to fill the space the elements occupied. The following code finds the element with and
var elmDeleted = document.getElementById("ads");
elmDeleted.parentNode.removeChild(elmDeleted);
1.7.3. Inserting an Element
Many user scripts insert content into a page, rather than appending it to the end of the page. The following code creates a link to
http://www.example.com
and
var elmNewContent = document.createElement('a');
elmNewContent.href = 'http://www.example.com/';
elmNewContent.appendChild(document.createTextNode('click here'));
var elmFoo = document.getElementById('foo');
elmFoo.parentNode.insertBefore(elmNewContent, elmFoo);
You can also insert content after an existing element, by using the nextSibling property: elmFoo.parentNode.insertBefore(elmNewContent, elmFoo.nextSibling);
1.7.4. Replacing an Element
You can replace entire
var elmNewContent = document.createElement('p');
elmNewContent.appendChild(document.createTextNode('Replaced!'));
var elmExtra = document.getElementById('extra');
elmExtra.parentNode.replaceChild(elmNewContent, elmExtra);
As you can see from the previous few examples, the process of creating new content can be arduous. Create an element, append some text, set individual attributes…bah. There is an easier way. It's not a W3C-approved DOM property, but all major browsers support the innerHTML property for getting or setting HTML content as a string. The following code accomplishes the same thing as the previous example:
var elmExtra = document.getElementById('extra');
elmReplaced.innerHTML = '<p>Replaced!</p>';
The HTML you set with the innerHTML property can be as complex as you like. Firefox will parse it and insert it into the DOM tree, just as if you had created each element and inserted it with standard DOM methods. 1.7.5. Modifying an Element's AttributesModifying a single attribute is simple. Each element is an object in JavaScript, and each attribute is reflected by a corresponding property. The following code finds the link with and changes its href property to link to a different URL:
var elmLink = document.getElementById('somelink');
elmLink.href = 'http://www.oreilly.com/';
You can accomplish the same thing with the setAttribute method:
elmLink.setAttribute('href', 'http://www.oreilly.com/')
This is occasionally useful, if you are setting an attribute whose
You can also remove an attribute entirely with the removeAttribute method:
elmLink.removeAttribute('href');
If you remove the
HRef
attribute from a link, it will still be an
<a>
element, but it will
|
| < Day Day Up > |