views:

59

answers:

2

I want to create an entire page through javascript i.e when i give a path in address bar of a browser the JavaScript will create the entire HTML page using document.write().

I just want to confirm whether it would be suitable doing this? Or if it would lead to any problems?

+3  A: 

It will lead to problems.

Firsty how are you going to maintain that code, it will be a nightmare.

Secondly, why on earth are you doing this? If it's to protect your super secret HTML, don't bother! It's not as valuable as you beleive it to be.

Thirdly, what about users without JS enabled?

Fourthly, how on earth are search engines going to index your site.

Fithly, as mentioned by oded, you need some sort of base page to call the script.

Tom Gullen
thnx.. i just wanted to know whether it is possible or not...????
suraj
@Tom: Rich web applications, like Google Maps, are totally rendered from JavaScript. You make some very valid points for the vast majority of web sites, but I don't think it is always the case... JavaScript code doesn't have to be a nightmare to maintain.
Daniel Vassallo
Sorry if my answer came off a little harsh. Most questions like this are to protect code which is impossible. Maintaining HTML code in a large bunch of document.writes will be a lot harder to maintain than standard HTML (although possibly not a nightmare as pointed out). From OP's answer I was also (possibly wrongly) assuming he wasn't talking about rich web apps, but your point is valid.If you want to know if it possible or not, yes it is totally possible, but unless used with caution it's going to lead to a lot of problems down the line.
Tom Gullen
+3  A: 

It is possible to serve an HTML page with an empty <body></body> tag, where all the elements are created in JavaScript. For example, some rich UI JavaScript frameworks, such as Sencha (previously called ExtJS), rely on this technique.

However, in general you wouldn't want to use document.write() for this. It's often better and easier to append your elements to the DOM with the appendChild() method, or by using the innerHTML property.

You may want to view the source of this example from Sencha, as an example. The whole UI is rendered in JavaScript:


As noted in the comments to your question, and in Tom's answer, you still need a base HTML page to serve the JavaScript code. The minimum you need is probably something like this:

<!DOCTYPE html>
<html> 
<head> 
   <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
   <title>My Rich Web Application</title> 
   <script src="your-code.js" type="text/javascript"></script> 
</head> 
<body> 
</body> 
</html>
Daniel Vassallo
thank Q very much..
suraj