|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
How do you get text to fit within a div?
Hi all,
I've been asked to update some old html pages that used to really interesting stuff. My problem is the page uses the function below to format the text within a div boundary. That's ok. But it does not work under firefox or anything other then IE. I was wondering if you have any alternative methods? Thanks, Bill Code:
//parse text to respect line breaks added - increases readability.
//lines beginning with a ">" character are presented with a monospace
//(fixed-width) font - e.g., so equations will appear correctly
function fix(e) {
var par = e.parentNode;
e.id = "";
var pos = e.innerText.indexOf("\n");
if (pos > 0) {
while (pos > 0) {
var t = e.childNodes(0);
var n = document.createElement("PRE");
var s = t.splitText(pos);
e.insertAdjacentElement("afterEnd", n);
n.appendChild(s);
e = n;
pos = e.innerText.indexOf("\n");
}
var count = (par.children.length);
for (var i = 0; i < count; i++) {
e = par.children(i);
if (e.tagName == "PRE") {
pos = e.innerText.indexOf(">");
if (pos != 0) {
n = document.createElement("DIV");
e.insertAdjacentElement("afterEnd", n);
n.innerText = e.innerText;
e.removeNode(true);
}
}
}
if (par.children.tags("PRE").length > 0) {
count = (par.childNodes.length);
for (i = 0; i < count; i++) {
e = par.children(i);
if (e.tagName == "PRE") {
e.id = "";
if (i < (count-1)) {
var e2 = par.children(i + 1);
if (e2.tagName == "PRE") {
e.insertAdjacentText("beforeEnd", e2.innerText+"\n");
e2.removeNode(true);
count = count-1;
i = i-1;
}
}
}
}
}
} else {
n = document.createElement("DIV");
par.appendChild(n);
n.innerText = e.innerText;
e.removeNode(true);
}
}
|
|
#3
|
|||
|
|||
|
Can someone help explain to me what this code is doing? I've been at it for quite some time here and still don't really get it.
|
|
#4
|
|||
|
|||
|
Code:
function adjust(e)
{
var curNode = document.getElementById(e);
var pNode = curNode.parentNode;
var pos = curNode.innerHTML.indexOf("\n");
//if there is a newline character.
if(pos > 0)
{
while(pos > 0)
{
var subNode = curNode.childNodes[0];
var preElement = document.createElement("PRE");
var split = subNode.splitText(pos);
pNode.insertBefore(preElement, curNode.nextSibling);
preElement.appendChild(split);
curNode = preElement;
pos = curNode.innerHTML.indexOf("\n");
}
var count = (pNode.children.length);
for(var i = 0; i < count; i++)
{
curNode = pNode.children(i);
if(curNode.tagName == "PRE")
{
pos = curNode.innerHTML.indexOf(">");
if(pos != 0)
{
preElement = document.createElement("DIV");
pNode.insertBefore(preElement, curNode.nextSibling);
preElement.innerHTML = curNode.innerHTML;
curNode.removeNode(true);
}
}
}
if(pNode.children.tags("PRE").length > 0)
{
count = pNode.childNodes.length;
for(i=0; i< count; i++)
{
curNode = pNode.children(i);
if(curNode.tagName =="PRE")
{
curNode.id = "";
if(curNode < (count-1))
{
var e2 = pNode.children(i+1);
if(e2.tagName == "PRE")
{
curNode.appendChild(e2.innerHTML+"\n");
e2.removeNode(true);
count = count - 1;
i = i - 1;
}
}
}
}
}
else
{
preElement = document.createElement("DIV");
pNode.appendChild(preElement);
preElement.innerHTML = curNode.innerHTML;
curNode.removeNode(true);
}
}
}
Hi all, I thought i managed to convert everything over to DOM standards, and it works in IE, but again...does not work in Firefox. |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > How do you get text to fit within a div? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|