Google Answers Logo
View Question
 
Q: line reformatting script in javascript ( Answered 5 out of 5 stars,   1 Comment )
Question  
Subject: line reformatting script in javascript
Category: Computers
Asked by: ia-ga
List Price: $3.00
Posted: 26 Nov 2002 07:31 PST
Expires: 26 Dec 2002 07:31 PST
Question ID: 114863
HTML document contains list of URLs (one URL-per-line), 
wrapped in DIV tags (*1), like this:

    <DIV ID="foo">
    http://1.com/1
    http://2.com/2
    http://3.com/3
    </DIV>

Write javasctipt function 'Reformat(ID)' which visually reformats
contents of the DIV as if it was written as:

    <A HREF="http://1.com/1"> http://1.com/1 </A>
    <A HREF="http://2.com/2"> http://2.com/2 </A>
    <A HREF="http://3.com/3"> http://3.com/3 </A>

, when called as Reformat("foo"). That's it.  
The shorter the function is, the better.

(*1) You may change tags DIV../DIV to other tags if it
simplifies the task, as long as tags remain short and simple.

(*2) HTML will also contains other contents which shall not be changed
Answer  
Subject: Re: line reformatting script in javascript
Answered By: nickyspag-ga on 26 Nov 2002 09:57 PST
Rated:5 out of 5 stars
 
First, you need to find the div object in the document. Use the
document.getElementById() method to do this:
	
	obj = document.getElementById('foo');
	
Then, you need to get the contents of the div as a string. Use the
innerHTML property to do this:

	text = obj.innerHTML;

If you look in the JavaScript reference, you'll see that the String
object has a method called split(). This breaks a string into pieces
and returns these pieces in an array. We'll need to split the contents
of the div by the newline character (\n) as follows:

	lines = text.split("\n");

Note: there may be carriage return / line feed problems depending on
whether you're in a Unix, Windows or Mac environment. If the file was
created on Windows, you'll probably need to split by "\r\n".

This returns an array of each line, which you can then loop through,
and compose a new string containing the <a> tags and a <br> at the
end:

	for (i=0; i<lines.length; i++)
	{
		newHTML = newHTML + '<a href="'+lines[i]+'">'+lines[i]+'</a><br>';
	}

Lastly, set the innerHTML property of the div to the new string we've
made:
	
	obj.innerHTML = newHTML;

I've included a sample script below.


Additional Links:
Netscape's DevEdge JavaScript Reference
http://developer.netscape.com/docs/manuals/communicator/jsref/
JavaScript Reference for String.split()
http://developer.netscape.com/docs/manuals/communicator/jsref/corea2.htm#1012977

Search Strategy:
javascript reference
://www.google.com/search?q=javascript+reference


<html>
<head>
	<title>JavaScript: Using String.split()</title>
</head>
<body>

<div id="foo">
http://www.salon.com/
http://developer.netscape.com/
http://www.iol.co.za/
</div>


<br>
<a href="javascript:Reformat('foo');">Reformat!</a>
<script language="JavaScript1.2" type="text/javascript">
function Reformat(divid)
{
	var obj = document.getElementById(divid);
	var text = obj.innerHTML;
	var newHTML = "";
	
	lines = text.split("\n");
	for (i=0; i<lines.length; i++)
	{
		newHTML = newHTML + '<a href="'+lines[i]+'">'+lines[i]+'</a><br>';
	}
	obj.innerHTML = newHTML;
}
</script>
</body>
</html>
ia-ga rated this answer:5 out of 5 stars
Excellent. Just what I asked for. 
Which line do I need to add to auto-execute the Reformat('foo') whenever 
script is loaded ?

Comments  
Subject: Re: line reformatting script in javascript
From: nickyspag-ga on 26 Nov 2002 23:25 PST
 
Hi

Change the body tag to:
<body onLoad="Reformat('foo');">

Thanks!

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy