JavaScript (HTML5): テキストを画面に表示

snippet-text

javascript: snippet-text.js

(function() {
	function SnippetText() {
	};
	SnippetText.prototype.addText = function(value) {
		var textNode = document.createTextNode(value);
		var p = document.createElement("p");
		p.appendChild(textNode);
		document.body.appendChild(p);
	};
	SnippetText.prototype.addAlignRightRedText = function(value) {
		var textNode = document.createTextNode(value);
		var p = document.createElement("p");
		p.appendChild(textNode);
		p.style.textAlign = "right";
		p.style.backgroundColor = "#ff0000";
		p.style.color = "white";
		p.style.fontWeight = "bold";
		document.body.appendChild(p);
	};
	
	function loaded() {
		window.snippet = new SnippetText();
		window.snippet.addText("Hello World");
		window.snippet.addAlignRightRedText("right alignment and red text");
	}
	
	if(document.addEventListener){
		document.addEventListener("DOMContentLoaded", loaded, false);
	} else if(/msie/.test(navigator.userAgent.toLowerCase())){
		try {
			document.documentElement.doScroll("left");
		} catch(error){
			setTimeout(arguments.callee, 0);
			return;
		}
		loaded();
	}
}).call(this);