By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,665 Members | 1,576 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,665 IT Pros & Developers. It's quick & easy.

Read HTML-code from file

P: n/a
Hi,

I want to write a script that (run from a file on the local HD) reads
another local html-file into a variable, then make some changes to it, and
then output the result in a way that makes it simple for the user to save
it again. And all of this should run in any current browser, not just IE.

I managed to access the code of the current document by
code = document.documentElement.innerHTML

But when I open another window with the file I want to edit, all I get is
<head></head><body></body>

For the output I tried to do document.write in an output window, but I
can't save the code I generate there, all I get is the sourcecode of my
script page (the opener of the page I try to save). Any hints?

Oh, just in case that matters, I'm using Galeon on Linux.

I hope someone out there can help me.

Thanks, Christian
Jul 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Christian Schmitt hu kiteb:

For the output I tried to do document.write in an output window, but I
can't save the code I generate there, all I get is the sourcecode of
my script page (the opener of the page I try to save). Any hints?


Yep. Use a programing language in an environment that actuially has
permission to write a file. This does not apply to javascript running in
teh context of a web browser.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

Jul 20 '05 #2

P: n/a


Christian Schmitt wrote:
I want to write a script that (run from a file on the local HD) reads
another local html-file into a variable, then make some changes to it, and
then output the result in a way that makes it simple for the user to save
it again. And all of this should run in any current browser, not just IE.

I managed to access the code of the current document by
code = document.documentElement.innerHTML

But when I open another window with the file I want to edit, all I get is
<head></head><body></body>

For the output I tried to do document.write in an output window, but I
can't save the code I generate there, all I get is the sourcecode of my
script page (the opener of the page I try to save). Any hints?

Oh, just in case that matters, I'm using Galeon on Linux.


This sounds all like a task for server side scripting with PHP or JSP or
ASP.
But I think Galeon is Mozilla based so maybe it offers
XMLHttpRequest
e.g.
if (typeof XMLHttpRequest != 'undefined') {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.html', false);
httpRequest.send(null);
alert(httpRequest.responseText);
}
That way you can read text pages from the same server the page with the
script is loaded from.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #3

P: n/a


On Fri, 21 Nov 2003, Fabian wrote:
Christian Schmitt hu kiteb:

For the output I tried to do document.write in an output window, but I
can't save the code I generate there, all I get is the sourcecode of
my script page (the opener of the page I try to save). Any hints?


Yep. Use a programing language in an environment that actuially has
permission to write a file. This does not apply to javascript running in
teh context of a web browser.


Well, for several reasons I'm limited to Javascript. And I don't want to
initiate the saving from the script, just create a document that the user
can save manually.

PL
Jul 20 '05 #4

P: n/a


On Fri, 21 Nov 2003, Martin Honnen wrote:


Christian Schmitt wrote:
I want to write a script that (run from a file on the local HD) reads
another local html-file into a variable, then make some changes to it, and
then output the result in a way that makes it simple for the user to save
it again. And all of this should run in any current browser, not just IE.

I managed to access the code of the current document by
code = document.documentElement.innerHTML

But when I open another window with the file I want to edit, all I get is
<head></head><body></body>

For the output I tried to do document.write in an output window, but I
can't save the code I generate there, all I get is the sourcecode of my
script page (the opener of the page I try to save). Any hints?

Oh, just in case that matters, I'm using Galeon on Linux.
This sounds all like a task for server side scripting with PHP or JSP or
ASP.


This all takes place locally, the script is not on a server.
But I think Galeon is Mozilla based so maybe it offers
XMLHttpRequest
e.g.
if (typeof XMLHttpRequest != 'undefined') {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.html', false);
httpRequest.send(null);
alert(httpRequest.responseText);
}
That way you can read text pages from the same server the page with the
script is loaded from.

XMLHttpRequest seems to be supported, but hangs the browser :-((
Perhaps because I try to read a local file?
Any other ideas?

PL
Jul 20 '05 #5

P: n/a


Christian Schmitt wrote:
On Fri, 21 Nov 2003, Martin Honnen wrote:
Christian Schmitt wrote:
I want to write a script that (run from a file on the local HD) reads
another local html-file into a variable, then make some changes to it, and
then output the result in a way that makes it simple for the user to save
it again. And all of this should run in any current browser, not just IE.

I managed to access the code of the current document by
code = document.documentElement.innerHTML

But when I open another window with the file I want to edit, all I get is
<head></head><body></body>

For the output I tried to do document.write in an output window, but I
can't save the code I generate there, all I get is the sourcecode of my
script page (the opener of the page I try to save). Any hints?

Oh, just in case that matters, I'm using Galeon on Linux.


This sounds all like a task for server side scripting with PHP or JSP or
ASP.

This all takes place locally, the script is not on a server.


I understand that you are attempting things locally, but it sounds more
like a task to be solved with PHP or ASP or JSP as there you don't run
into the restrictions client side JavaScript has to access the file
system or any Web server.
But I think Galeon is Mozilla based so maybe it offers
XMLHttpRequest
e.g.
if (typeof XMLHttpRequest != 'undefined') {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.html', false);
httpRequest.send(null);
alert(httpRequest.responseText);
}
That way you can read text pages from the same server the page with the
script is loaded from.


XMLHttpRequest seems to be supported, but hangs the browser :-((
Perhaps because I try to read a local file?


The third argument to the open() call is the asynchronous mode, in the
example above that is false meaning the send() call later will block the
beowser till the file is loaded.
You can also perform asynchronous requests, then you need to set up an
httpRequest.onreadystate
handler.

If you do not even want to read files via http but only from the local
file system then perhaps XPConnect can do that with Galeon
http://www.faqts.com/knowledge_base/...d/23360/fid/53

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #6

P: n/a


On Fri, 21 Nov 2003, Martin Honnen wrote:
But I think Galeon is Mozilla based so maybe it offers
XMLHttpRequest
e.g.
if (typeof XMLHttpRequest != 'undefined') {
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'whatever.html', false);
httpRequest.send(null);
alert(httpRequest.responseText);
}
That way you can read text pages from the same server the page with the
script is loaded from.

XMLHttpRequest seems to be supported, but hangs the browser :-((
Perhaps because I try to read a local file?


The third argument to the open() call is the asynchronous mode, in the
example above that is false meaning the send() call later will block the
beowser till the file is loaded.


That much I found out already, but why doesn't it load at all? Loading a
small local file should happen instantly.
You can also perform asynchronous requests, then you need to set up an
httpRequest.onreadystate
handler.

If you do not even want to read files via http but only from the local
file system then perhaps XPConnect can do that with Galeon
http://www.faqts.com/knowledge_base/...d/23360/fid/53


But this works only in Mozilla-based browsers, right? I need something
that works in ALL (well, most of the common ones, at least) current
browsers on all platforms.

Christian
Jul 20 '05 #7

P: n/a
VK
> But this works only in Mozilla-based browsers, right? I need something
that works in ALL (well, most of the common ones, at least) current
browsers on all platforms.


Platform independent bugs-free 99% reliable local File I/O interface? If
anyone gets a solution of this, don't post it here, but sell to Sun Java
for a good money. They are poking on the roses with it for a decade now,
and it's still buggy like hell. Too many issues are involved here, and
it would take a dozen of pages just to explain them (besides technical,
a lot of security related).

You have an easy solution with Windows systems with IE installed. Its
implementation of JavaScript (JScript) supports ActiveXObject, which
works just fine with local files. That would cover about 85% of the
potential customers.

The only totally universal solution would be to make a JavaScript editor
and provide an event-driven browser-dependent prompt like "Choose File >
Open to open the file you want to edit" and "Choose File > Save As to
save the changes you've made".
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.