/    Sign up×
Community /Pin to ProfileBookmark

Javascript innerHTML

in my index.html page i have the following code:

[CODE]
<html>
<body>
<table border=”0″ >
<tr >
<td valign=”top” align=”left”>

<script type=”text/javascript” src=”test.js”></script>

</td>

<td valign=”top” id =”test”>

</td>
</tr>
</table>
</body>
</html>
[/CODE]

in the left cell of the table a menu is included with <script type=”text/javascript” src=”test.js”></script>

test.js contains a menu with for example this link:

<a href=”#” onclick=”setPage(‘multimedia’)”><span class=”style2″>:::Multimedia</span></a></td></tr>’);

when clicked on this link the setPage() function is called and the multimedia page should be loaded in the right cell of the table.

The function is as followed :

function setPage(f) {

if(f == “multimedia”) {

var cell = document.getElementById(“test”);
cell.innerHTML = “<script src=’test2.js’> </script>”;

}

}

My problem is that cell.innerHTML = “<script src=’test2.js’> </script>”; doesn’t work. Does somebody know what I do wrong ? please help!

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 25.2007 — var cell = document.getElementById("test");
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.src = 'test2.js';
cell.appendChild(js);
Copy linkTweet thisAlerts:
@faridvwauthorSep 25.2007 — Thanx for your reaction but it doesn't work .
Copy linkTweet thisAlerts:
@FangSep 25.2007 — What is the contents of test2.js ?
Copy linkTweet thisAlerts:
@faridvwauthorSep 25.2007 — this is te code of test2.js :

[CODE]
document.write('<style type="text/css"> <!-- .style3 {font-family: Verdana, Arial, Helvetica, sans-serif} --> </style>');
document.write('<html><head><title>Test</title></head>');
document.write('<body><table border="0" bordercolor="#000000" ><td valign="top"><blockquote><span class="style3"><h1><center>This is a test</center></h1> </span>');
document.write('<p align="justify" class="style3"> THis is just a test</p></blockquote></td>');
[/CODE]
Copy linkTweet thisAlerts:
@FangSep 25.2007 — Using [I]document.write[/I] after the page is loaded will result in a new page. The original page will no longer exist.
Copy linkTweet thisAlerts:
@faridvwauthorSep 25.2007 — So what should I use instead of document.write ?
Copy linkTweet thisAlerts:
@FangSep 25.2007 — index.htm&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;content&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;
&lt;script type="text/javascript"&gt;
function setPage(f) {
if(f == "multimedia") {
var cell = document.getElementById("test");
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.src = 'test2.js';
cell.appendChild(js);
}
}
&lt;/script&gt;

&lt;style type="text/css"&gt;
.style3 {font-family: Verdana, Arial, Helvetica, sans-serif}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;table border="0" &gt;
&lt;tr &gt;
&lt;td valign="top" align="left"&gt;

<i> </i>&lt;script type="text/javascript" src="test.js"&gt;&lt;/script&gt;

<i> </i>&lt;/td&gt;


&lt;td valign="top" id ="test"&gt;


&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

test2.jsvar txt='&lt;body&gt;&lt;table border="0" bordercolor="#000000" &gt;&lt;td valign="top"&gt;&lt;blockquote&gt;&lt;span class="style3"&gt;&lt;h1&gt;&lt;center&gt;This is a test&lt;/center&gt;&lt;/h1&gt; &lt;/span&gt;';
txt+='&lt;p align="justify" class="style3"&gt; THis is just a test&lt;/p&gt;&lt;/blockquote&gt;&lt;/td&gt;';
document.getElementById("test").innerHTML=txt;
Copy linkTweet thisAlerts:
@faridvwauthorSep 25.2007 — Thank you very much. It works now!
Copy linkTweet thisAlerts:
@faridvwauthorSep 25.2007 — THere is only one problem if I click twice on the same link an win32 exeption occurs
Copy linkTweet thisAlerts:
@FangSep 25.2007 — Changing test.js will prevent the second event:document.write('&lt;a href="#" onclick="setPage('multimedia');[COLOR="Green"]this.onclick=null;[/COLOR]"&gt;&lt;span class="style2"&gt;:::Multimedia&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;');
What you are attempting is not very practical. If this is to show and hide text, then there are better ways.
×

Success!

Help @faridvw spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...