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
  • 3
    How do you plan to load JS into the browser? Commented Aug 23, 2010 at 11:35
  • 2
    Is there any particular reason why you have taken this approach? Or is this just to see if it can be done? Commented Aug 23, 2010 at 11:37
  • 2
    This is a terrible idea. Why do you want to do this? Commented Aug 23, 2010 at 11:38

2 Answers 2

3

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>
Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.