Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-10-2012, 07:25 PM   PM User | #1
jason_kelly
Regular Coder

 
Join Date: Sep 2011
Posts: 140
Thanks: 88
Thanked 0 Times in 0 Posts
jason_kelly is an unknown quantity at this point
HTML to Javascript String Converter

Hello,

I am trying to design and develop a custom HTML to JavaScript converter so as to save time and increase effiency as opposed to having to write out every single line of html.

I refer to this website: http://www.neuetech.com/tools.php?function=8

I am trying to do something similar, but since i'd like more control of writting it dynamically because I will insert if statements later but, how can the code be modified such that the output would be as follows

Desired result:
Code:
var html = ''
html += '<html>\n'
html += '<head>\n'
ect.
and not:
Code:
var html = '<html>\n' +
'<html>\n' +
'<head>\n' +
Here is my current code which does not work to begin with, I can't seem to figure it out, by my eye all should be fine and dandy.

Code:
<html>

<head>

<script type="text/javascript">
function test() {

var oldtext = document.getElementById('oldtext').value
    oldtext = RegExReplace(oldtext, '\'', '\\\'');
    oldtext = RegExReplace(oldtext, '^\\s+', '');
    oldtext = RegExReplace(oldtext, '\n\\s+', '\n');
    oldtext = RegExReplace(oldtext, '\r', '');
    oldtext = RegExReplace(oldtext, '\n', '\\n\' +\n    \'');
    oldtext = RegExReplace(oldtext, '</', '<\\/');
    oldtext = 'html = \'' + oldtext + '\';';

alert(oldtext);    

}

</script>    
</head>    
<body>

<textarea id="oldtext"></textarea>
<br><br>
<textarea id="newtext"></textarea>
<br><br>
<input type="submit" value="Convert" onclick="test()">

</body>
</html>
jason_kelly is offline   Reply With Quote
Old 12-10-2012, 08:14 PM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
If that is your current code then it won't work because you haven't included the function RegExReplace.

But, personally, I can't see how your third code sample relates directly to the previous two?! Perhaps it's just me.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 12-10-2012, 08:37 PM   PM User | #3
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
Not to mention that the form will submit unless OP prevents default action. Best to replace "SUBMIT" with "BUTTON".
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 12-10-2012, 08:42 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,530
Thanks: 0
Thanked 503 Times in 494 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
If you are loooking at seriously generating a significant amount of HTML from JavaScript then your best option would be to use the actual DOM commands such as createElement() and appendChild() to build the HTML as then you will not need to keep track of where the closing tags are supposed to go as the HTML will be generated with the correct syntax automatically.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-11-2012, 02:13 AM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,553
Thanks: 9
Thanked 480 Times in 463 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
personally, i would use templates.


simplest ex:
Code:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>template test</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	
</head>
<body>

<input value='hello' type='button'   onclick="main.innerHTML=template(pages[0], templateString)" />	
<input value='goodbye' type='button'   onclick="main.innerHTML=template(pages[1], templateString)" />	

<div id='main'>
	<h1>{title}</h1>	
	<p>{description}</p>
</div>	  
 
<script type='text/javascript'>

var pages=[
 { title: "hello", description: "hello world hello world hello world hello world hello world "} ,
 { title: "goodbye", description: "goodbye world goodbye world goodbye world goodbye world goodbye world goodbye world "} 
]


var templateString=main.innerHTML;


function template(ob, str){
   return str.replace(/{([\w\s.$_]+?)}/g, 
     function(j,a){  return ob[a]||"";  } );
}


</script>
</body>
</html>
look into handlebars for a lot more features or knockout for live data I/O binding.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
jason_kelly (12-12-2012)
Reply

Bookmarks

Tags
javascript

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:49 AM.


Advertisement
Log in to turn off these ads.