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?
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:
- http://dev.sencha.com/deploy/dev/examples/forum/forum.html
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>
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.
精彩评论