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

Load Body html on button click.

P: n/a
Hi,

Can someone tell me, How to load the Body Html from a text file that
contains javascript.
to Manage my files I am creating an Index Page.
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>

</head>
<body onUnload ="WriteCSVFile();">

<script type="text/javascript">

function openWindow(openLink)
{
var abc = openLink;
window.open(abc);
}

function openWindow2()
{
alert(textnode1.value);
var abc = '"' + textnode1.value + '"'
//window.open(eval(abc));
alert(document.body.innerHTML);
}

var textnode1;
var textnode2;
function addRow(content,morecontent)
{
if (!document.getElementsByTagName('mytable')) return;
var tbl = document.getElementById('mytable');
tabBody=document.getElementsByTagName("TBODY").ite m(0);
var lastRow = tbl.rows.length;
var iteration = lastRow;
//alert(iteration);
row=document.createElement("TR");
cell1 = document.createElement("TD");
cell2 = document.createElement("TD");
textnode1=document.createElement('input');
textnode1.type = 'text';
textnode1.name = 'txtRow' + iteration;
textnode1.id = 'txtRow' + iteration;
textnode1.size = 80;
var answer = prompt("Enter the URL for Map.","");
textnode1.value =answer;
textnode2=document.createElement('input');
textnode2.type = 'button';
textnode2.name = 'txtRowBut' + iteration;
textnode2.id = 'txtRowBut' + iteration;
textnode2.width = "100"
textnode2.value = "New Map"
cell1.appendChild(textnode1);
cell2.appendChild(textnode2);
row.appendChild(cell1);
row.appendChild(cell2);
tabBody.appendChild(row);
document.all.txtRowBut5.attachEvent("onclick", openWindow2)
}

function WriteCSVFile()
{
var ForWriting = 2
var TriStateFalse = 0
var fsObj = new ActiveXObject("Scripting.FileSystemObject")
var newFile = fsObj.OpenTextFile("c:\\abc.txt", ForWriting, true,
TriStateFalse)
newFile.WriteLine(document.body.innerHTML)
newFile.Close()

}
</script>

<div id="plan" style="overflow: auto; width: 780px; height: 780px">
<div id="DateBox">
<script type="text/javascript">
document.write('<b>' + Date());
</script>
</div>
<br/>
<br/>
<br/>
<br/><br/>

<table id='mytable' style="font-family:Arial,Helvetica,Sans Serif;font-
size:90%;color:#330066" cellpadding="3" border="2">
<H3><B><U>INDEX</U></B></H3>
<tbody>
<tr><td><input type="TEXT" name="testlabels" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels').value);">
</td></tr>
<tr><td><input type="TEXT" name="testlabels1" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels1').value);">
</td></tr>
<tr><td><input type="TEXT" name="testlabels2" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels2').value);">
</td></tr>
<tr><td><input type="TEXT" name="testlabels3" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels3').value);">
</td></tr>
</tbody>
</table>
</form>
<INPUT TYPE="button" VALUE="Add Map" onClick="addRow();return false;">
<br/>
</div>
</body>
</html>
************************************************** ************************************************** *
At the Body unload I am taking the Body InnerHtml & saving it in a
File. abc.txt. As I am adding the Links on the fly to the table, so I
need some place to save them. I want that when Next time, I open this
webpage, It should automatically, read all the script that I saved to
abc.txt file. Or Is there a better way to add the contents & save it.
Sep 29 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
hi,
you can try these:

---------------------------------------------------------------------------------
xmldoc = new ActiveXObject("Msxml2.DOMDocument");
xmldoc.onreadystatechange = CheckState;
xmldoc.resolveExternals = false;
xmldoc.load("abc.txt");

function CheckState(){
var state = xmldoc.readyState;
if (state != 4){
return;
}

var xmlObj=xmldoc.documentElement;
// TODO process the file content
}
---------------------------------------------------------------------------------

tips:
1. not sure whether only XML file can be accepted by the ActiveXObject
2. not sure absolute path is available for "xmldoc.load("...");"

Sunny wrote:
Hi,

Can someone tell me, How to load the Body Html from a text file that
contains javascript.
to Manage my files I am creating an Index Page.
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>

</head>
<body onUnload ="WriteCSVFile();">

<script type="text/javascript">

function openWindow(openLink)
{
var abc = openLink;
window.open(abc);
}

function openWindow2()
{
alert(textnode1.value);
var abc = '"' + textnode1.value + '"'
//window.open(eval(abc));
alert(document.body.innerHTML);
}

var textnode1;
var textnode2;
function addRow(content,morecontent)
{
if (!document.getElementsByTagName('mytable')) return;
var tbl = document.getElementById('mytable');
tabBody=document.getElementsByTagName("TBODY").ite m(0);
var lastRow = tbl.rows.length;
var iteration = lastRow;
//alert(iteration);
row=document.createElement("TR");
cell1 = document.createElement("TD");
cell2 = document.createElement("TD");
textnode1=document.createElement('input');
textnode1.type = 'text';
textnode1.name = 'txtRow' + iteration;
textnode1.id = 'txtRow' + iteration;
textnode1.size = 80;
var answer = prompt("Enter the URL for Map.","");
textnode1.value =answer;
textnode2=document.createElement('input');
textnode2.type = 'button';
textnode2.name = 'txtRowBut' + iteration;
textnode2.id = 'txtRowBut' + iteration;
textnode2.width = "100"
textnode2.value = "New Map"
cell1.appendChild(textnode1);
cell2.appendChild(textnode2);
row.appendChild(cell1);
row.appendChild(cell2);
tabBody.appendChild(row);
document.all.txtRowBut5.attachEvent("onclick", openWindow2)
}

function WriteCSVFile()
{
var ForWriting = 2
var TriStateFalse = 0
var fsObj = new ActiveXObject("Scripting.FileSystemObject")
var newFile = fsObj.OpenTextFile("c:\\abc.txt", ForWriting, true,
TriStateFalse)
newFile.WriteLine(document.body.innerHTML)
newFile.Close()

}
</script>

<div id="plan" style="overflow: auto; width: 780px; height: 780px">
<div id="DateBox">
<script type="text/javascript">
document.write('<b>' + Date());
</script>
</div>
<br/>
<br/>
<br/>
<br/><br/>

<table id='mytable' style="font-family:Arial,Helvetica,Sans Serif;font-
size:90%;color:#330066" cellpadding="3" border="2">
<H3><B><U>INDEX</U></B></H3>
<tbody>
<tr><td><input type="TEXT" name="testlabels" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels').value);">
</td></tr>
<tr><td><input type="TEXT" name="testlabels1" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels1').value);">
</td></tr>
<tr><td><input type="TEXT" name="testlabels2" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels2').value);">
</td></tr>
<tr><td><input type="TEXT" name="testlabels3" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels3').value);">
</td></tr>
</tbody>
</table>
</form>
<INPUT TYPE="button" VALUE="Add Map" onClick="addRow();return false;">
<br/>
</div>
</body>
</html>
************************************************** ************************************************** *
At the Body unload I am taking the Body InnerHtml & saving it in a
File. abc.txt. As I am adding the Links on the fly to the table, so I
need some place to save them. I want that when Next time, I open this
webpage, It should automatically, read all the script that I saved to
abc.txt file. Or Is there a better way to add the contents & save it.
Sep 30 '08 #2

P: n/a
On Sep 30, 6:38*am, Sunny <sunnyluth...@gmail.comwrote:
Hi,

Can someone tell me, How to load the Body Html from a text file that
contains javascript.
to Manage my files I am creating an Index Page.
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title></title>

</head>
<body onUnload ="WriteCSVFile();">

<script type="text/javascript">

function openWindow(openLink)
{
var abc = openLink;
window.open(abc);
}

function openWindow2()
{
alert(textnode1.value);
var abc = '"' + textnode1.value + '"'
//window.open(eval(abc));
alert(document.body.innerHTML);

}

var textnode1;
var textnode2;
*function addRow(content,morecontent)
{
* * * * * if (!document.getElementsByTagName('mytable')) return;
There are no HTML elements with a tagName of 'mytable', did you mean
to do something like:

if (!document.getElementById) return;

or perhaps:

var tbl;
if ( !document.getElementById ||
!(tbl = document.getElementById('mytable')) {
return;
}

* * * * * * * * * * * * var tbl = document.getElementById('mytable');
* * * *tabBody=document.getElementsByTagName("TBODY").it em(0);
Since you already have a reference to the table, consider:

var tabBody = tbl.tBodies[0];

* * * * * * * * * * * * *var lastRow = tbl.rows.length;
* * * * * * * * * * * * *var iteration = lastRow;
* * * * * * * * * * * * *//alert(iteration);
* * * * * * * row=document.createElement("TR");
* * * * * * *cell1 = document.createElement("TD");
* * * * * * *cell2 = document.createElement("TD");
* * * * * * *textnode1=document.createElement('input');
* * * * * * * * * * * * * * * * * * * * * * * * *textnode1.type = 'text';
Please manually wrap code for posting at about 70 characters to
prevent auto-wrapping. Also, indent using 2 or 4 spaces to keep code
compact.

Why are those variables allowed to become global? Keep them local
with var.

* * * * * * *textnode1.name = 'txtRow' + iteration;
* * * * * * *textnode1.id = 'txtRow' + iteration;
* * * * * * *textnode1.size = 80;
* * * * * * * * * * * * * * * * * * * * * * * * *var answer = prompt("Enter the URL for Map.","");
* * * * * * * * * * * * * * * * * * * * * * * * *textnode1.value =answer;
* * * * * * *textnode2=document.createElement('input');
* * * * * * * * * * * * * * * * * * * * * * * * *textnode2.type = 'button';
* * * * * * *textnode2.name = 'txtRowBut' + iteration;
* * * * * * *textnode2.id = 'txtRowBut' + iteration;
* * * * * * * * * * * * * * * * * * * * * * * * *textnode2.width = "100"
* * * * * * * * * * * * * * * * * * * * * * * * *textnode2.value = "New Map"
* * * * * * *cell1.appendChild(textnode1);
* * * * * * cell2.appendChild(textnode2);
* * * * * * row.appendChild(cell1);
* * * * * * *row.appendChild(cell2);
* * * * * * tabBody.appendChild(row);
document.all.txtRowBut5.attachEvent("onclick", openWindow2)
document.all is IE proprietary, use getElementById.
attachEvent is IE proprietary, there are plenty of cross-browser
solutions, search the archives for "addEventListener attachEvent".

}

function WriteCSVFile()
{
* *var ForWriting = 2
* *var TriStateFalse = 0
* *var fsObj = new ActiveXObject("Scripting.FileSystemObject")
* * var newFile = fsObj.OpenTextFile("c:\\abc.txt", ForWriting, true,
TriStateFalse)
More IE specific stuff. There are examples of how to do cross-browser
AJAX in the archives.

* * * * *newFile.WriteLine(document.body.innerHTML)
* *newFile.Close()
}

</script>

<div id="plan" *style="overflow: auto; width: 780px; height: 780px">
<div id="DateBox">
<script type="text/javascript">
*document.write('<b>' + Date());
</script>
</div>
<br/>
Forget the XHTML syntax as you are clearly using HTML.

<br/>
<br/>
<br/><br/>

<table id='mytable' style="font-family:Arial,Helvetica,Sans Serif;font-
size:90%;color:#330066" cellpadding="3" border="2">
<H3><B><U>INDEX</U></B></H3>
Try validating your HTML before doing anything, a table element can
only have table section elements as children, it can't have an h3 as a
child.

*<tbody>
<tr><td><input type="TEXT" name="testlabels" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels').value);">
* * * * </td></tr>
<tr><td><input type="TEXT" name="testlabels1" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels1').value);">
* * * * </td></tr>
* * * * <tr><td><input type="TEXT" name="testlabels2" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels2').value);">
* * * * </td></tr>
* * * * * * * * <tr><td><input type="TEXT" name="testlabels3" style="width:510px"
value=""></td>
<TD><INPUT TYPE="button" VALUE=""
onClick="openWindow(document.getElementById('testl abels3').value);">
* * * * </td></tr>
* * * * * * * * *</tbody>
* * * * </table>
</form>
Where is the opening tag for the form?
<INPUT TYPE="button" VALUE="Add Map" onClick="addRow();return false;">
The "return false" here does nothing useful.

<br/>
</div>
</body>
</html>
************************************************** ************************************************** *
At the Body unload I am taking the Body InnerHtml & saving it in a
File. abc.txt. As I am adding the Links on the fly to the table, so I
need some place to save them. I want that when Next time, I open this
webpage, It should automatically, read all the script that I saved to
abc.txt file. Or Is there a better way to add the contents & save it.
The innerHTML property is proprietary, there is no public standard for
its use. Different browsers have implemented it differently, so it is
inconsistent. I better idea would be to serialise the data you want
to keep, then parse it back into the document when it is re-loaded.

Have a look at TiddlyWiki to see how they've done it.

<URL: http://www.tiddlywiki.com/ >
--
Rob
Sep 30 '08 #3

P: n/a
Hi, Instead of using cookies.

I am using a database to store the elements on form.
And at body load I am reading the elements from database.
The only problem is when I am reading the elements I am attaching the
onclick event to the buttons on form. whenever i click any button on
form, it refer to the value of last element. So all the button clicks
open a same link.
and when i pass parameter to the function that i am attaching with
onclick event & when i load the page. the link opens automatically.

Is there a way to detect which button was clicked & the link dont open
automatically on page load.
Here is the code so far:
while(!RS3.eof) {
if (!document.getElementsByTagName('mytable')) return;
var tbl = document.getElementById('mytable');
tabBody=document.getElementsByTagName("TBODY").ite m(0);
var lastRow = tbl.rows.length;
var iteration = lastRow;
//alert(iteration);
row=document.createElement("TR");
cell1 = document.createElement("TD");
cell2 = document.createElement("TD");
textnode1=document.createElement('input');
textnode1.type = RS3.fields(3).value;
textnode1.name = RS3.fields(1).value;
textnode1.id = RS3.fields(2).value;
textnode1.size = RS3.fields(4).value;
textnode1.value = RS3.fields(5).value;

textnode[RS3.recordcount]=document.createElement('input');
textnode[RS3.recordcount].type = RS3.fields(8).value;
textnode[RS3.recordcount].name = RS3.fields(6).value;
var onClickname = "'" + RS3.fields(6).value + "'";
textnode[RS3.recordcount].id = RS3.fields(7).value;
textnode[RS3.recordcount].width = RS3.fields(9).value
textnode[RS3.recordcount].value = RS3.fields(10).value;
cell1.appendChild(textnode1);
cell2.appendChild(textnode[RS3.recordcount]);
row.appendChild(cell1);
row.appendChild(cell2);
tabBody.appendChild(row);
RS3.MoveNext();
textnode[RS3.recordcount].attachEvent('onclick',openWindow2);
}
}

function openWindow2(linkURL,elmClicked)
{
//alert(textnode1.value);
//var abc = '"' + textnode1.value + '"'
//if (window.event.srcElement == elmClicked)
//{
var abc = '"' + textnode1.value + '"'
window.open(eval(abc));
//alert(document.body.innerHTML);
//}
}
Sep 30 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.