JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 7th, 2006, 02:20 PM
bzborow1 bzborow1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 85 bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 23 m 10 sec
Reputation Power: 8
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);
		}
	}

Reply With Quote
  #2  
Old April 7th, 2006, 02:50 PM
Kravvitz's Avatar
Kravvitz Kravvitz is offline
CSS & JS/DOM Adept
Dev Shed God 23rd Plane (16000 - 16499 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 16,407 Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level)Kravvitz User rank is General 12nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Months 1 Day 14 h 19 m 23 sec
Reputation Power: 1780
__________________
Spreading knowledge, one newbie at a time.

Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

Check out my blog.

Reply With Quote
  #3  
Old April 10th, 2006, 05:07 PM
bzborow1 bzborow1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 85 bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 23 m 10 sec
Reputation Power: 8
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.

Reply With Quote
  #4  
Old April 11th, 2006, 11:51 AM
bzborow1 bzborow1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2003
Location: Canada
Posts: 85 bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level)bzborow1 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 18 h 23 m 10 sec
Reputation Power: 8
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > How do you get text to fit within a div?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT