...

View Full Version : Can javascript write an html page and then open it in a broswer?



cmasterd
11-21-2012, 05:20 PM
I wanted to know if I can make a html page with document.write and then open it in a browers window lets say IE? Anyone know how I can do this?

Philip M
11-21-2012, 05:28 PM
I wanted to know if I can make a html page with document.write and then open it in a browers window lets say IE? Anyone know how I can do this?

Yes, it is possible. document.write() can be used to write the original content of the page (but not to update it after the page has loaded). But it is strongly contra-indicated, as the Americans say. Obviously the browser must be running and Javascript must be enabled before any document.write() statements can be executed.

What are you actually trying to do? What exactly is the point? What would that achieve?
The most peculiar and bizarre requests seem to come from complete newcomers.


It is your responsibility to die() if necessary….. - PHP Manual

VIPStephan
11-21-2012, 05:30 PM
Also, JS availability aside, it’s a pretty complicated way to construct a website like that. I’m also curious what the intention is.

cmasterd
11-21-2012, 05:49 PM
Pretty much its for testing, the end game is that I can put in a file path to a html page which is local. Within that html page is a div tag that I want to take and put into the js made html page. Its so that I can take a div that that changes all the time and place it in a test page that never changes. So I know how it will look on a local version of the live site. It would just be a handy tool.

felgall
11-21-2012, 07:39 PM
Using any server side language you can do the same thing in a way that works for everyone and not just those with JavaScript.

Old Pedant
11-21-2012, 11:21 PM
<shrug>You could do this using document.write, but why?</shrug>

If all you need to do is clone the contents of a <div> into another window, there's no need to use document.write.

Let's say you have a basically blank HTML page that looks like this:


<html>
<body>
<div id="putItHere"></div>
</body>
</html>

Let's name that HTML file "myBlank.html".

So now, from your main page, you could do something like this:


<html>
<body>
<div id="whatever">
Any content at all.
<div> even an embedded div </div>
</div>

<input type="button" id="doit" value="Clone!"/>

<script type="text/javascript">
(
function( )
{
document.getElementBYId("doit").onclick = cloneIt;

function cloneIt( )
{
var w = window.open( "myBlank.html", "_blank" );
w.onload =
function( )
(
document.getElementById("putItHere").innerHTML =
window.opener.document.getElementById("whatever").innerHTML;
}
}
}
)( );
</script>
</body>
</html>


Not the best possible way, but it should work. And certainly better then document.write().

cmasterd
11-22-2012, 02:01 AM
I wanted it to work with a c++ program which lets you preview the html page you want to take the div tag out of and then it runs the javascript which place it into a template html page. I thought that if the JS just set up the template page it would be easier rather then taking it out of the first page opening a second page and place it in it.

felgall
11-22-2012, 02:11 AM
I wanted it to work with a c++ program which lets you preview the html page you want to take the div tag out of

So why not have the C++ do the whole thing rather than using JavaScript to do something that is better done on the server.

cmasterd
11-22-2012, 11:56 AM
Good way of learning Javascript?

Old Pedant
11-22-2012, 05:58 PM
Debatable.

What you are trying to do isn't something one would normally use JS for.

Even the code I provided, using innerHTML to copy the contents of a <div>, isn't best case JavaScript usage. And document.write() most certainly isn't.

felgall
11-22-2012, 07:50 PM
Good way of learning Javascript?

Worst possible way since the commands you would need to use are basically obsolete in JavaScript having been replaced by server side coding a long time ago.

As server side processing can handle almost everything prior to the web page finishing loading, modern JavaScript assumes that the HTML has already loaded prior to any of it running and trying to interact with the page.

You should always use the language and language constructs intended for a particular task. There are enough legitimate uses for JavaScript and the particular commands in any language to not need to "invent" situations to learn them that in the real world ought to be handled either by a different language or a different command.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum