Forum Moderators: open

Message Too Old, No Replies

Problem saving file as htm

Javscript writes html but there is a problem....

         

monaghan

3:02 pm on May 1, 2009 (gmt 0)

10+ Year Member



Group,

From the client side, I need to write and save a separate html file (not text because I need a hyperlink). The following code works okay but the page it generates, when viewing the source, places the writing content in a <pre> which will not allow me to write a hyperlink. Any thoughts?

Thanks!

<script>
<!--
function savefile( f ) {
f = f.elements;
var w = window.frames.w;
if( !w ) {
w = document.createElement( 'iframe' );
w.id = 'w';
w.style.display = 'none';
document.body.insertBefore( w );
w = window.frames.w;
if( !w ) {
w = window.open( '', '_temp', 'width=100,height=100' );
if( !w ) {

window.alert( 'Sorry, could not create file.' ); return false;
}
}
}
var d = w.document,
ext = f.ext.options[f.ext.selectedIndex],
name = f.filename.value.replace( /\//g, '\\' ) + ext.text;
d.open( 'text/plain', 'replace' );
d.charset = ext.value;
if( ext.text==='.txt' ) {
d.close();
} else {//'.htm'

d.write('\r\n');
d.write( f.field1.value );
d.write('\r\n');
d.write( f.field2.value );
d.write('\r\n');
d.write('<a href=');
d.write( f.field3.value );
d.write('>');
d.write( f.field3.value );
d.write('</a>');
d.write('\r\n');
d.close();
}
if( d.execCommand( 'SaveAs', null, name ) ){
window.alert( name + ' has been saved.' );
} else {
window.alert( 'The file has not been saved.\nIs there a problem?' );
}
w.close();
return false;
}
-->
</script>

<form action="#" onsubmit="return savefile(this);">

<fieldset>
<legend>New Record</legend>

<table>
<tr>
<td class="left">Field 1:</td>
<td class="right">
<input type="text" name="field1" accesskey="1" value="">
</td>
</tr>

<tr>
<td class="left">Field 2:</td>
<td class="right">
<input type="text" name="field2" accesskey="2" value="">
</td>
</tr>

<tr>
<td class="left">Field 3:</td>
<td class="right">
<input type="text" name="field3" accesskey="3" value="">
</td>
</tr>

</table>

<div>

<input size="40" type="text" name="filename" value="" accesskey="a"/>

<select name="ext" title=" Extension ">
<option value="iso-8859-1" selected>.htm</option>
</select>
&nbsp;

<br>
<button type="submit" accesskey="s">Save</button>
<br>
<button type="reset" accesskey="r">Reset</button>
<br>
</div>
</fieldset>

</form>

whoisgregg

3:46 pm on May 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, monaghan!

I have no experience using Javascript to write files to the local file system (since it's a IE only feature), but since no one else has taken a crack at it, I wonder if this line is at fault?

d.open( 'text/plain', 'replace' );

Perhaps it should be an HTML MIME type, like so:

d.open( 'text/html', 'replace' );

HTH