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

Should be simple!

P: 3
I'm trying to do something that should be very simple but isn't working! (I also want to do something somewhat more complicated, but that has to wait until I figure out the simple stuff.)

First, I want to call out a .js file from inside a web page to write some stuff. I've done it before, but it's been a while. I've got it ultra-simplified just to try to get it to work. (Once I've got the bugs out, I'm going to put some real code in the .js file.)

[HTML]
This is what's in my web page:

<div id=content>
<p>

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

</p>
</div>
[/HTML]
-------------------------

Here's the content of outback.js:

Expand|Select|Wrap|Line Numbers
  1. document.writeln("We went to the outback.");
-------------------------

When I refresh the web page, nothing appears (except what is actually written inside the page). If I get rid of the reference to outback.js and put the writeln statement inside the <script></script> tags, I get what I want. But I don't want to do that because ... here's the second, advanced question:

I would like to be able to swap out the text that's inside of those <div></div> tags, based on a function triggered by an onClick event in another div. So, I want to have a little menu and based on what someone clicks ("outback", "daintree", "whitsundays", etc.), they get different text (and more things to click on) in the "content" div.

The question is whether this is possible -- I have read that you can't change text in an existing page, but why can't you? You can change images. I've already got my page set up so that when you click different links in one div, you see different images in another div. I just want to modify the swap-out function that changes the images so that it changes the reference to the .js file.

So,

First question: Why can't I get the writeln command to execute when it's in the .js file?

Second question: Can I swap out the contents of a .js file within a given div?

Thanks for any assistance.
Jan 27 '08 #1
Share this Question
Share on Google+
5 Replies


gits
Expert Mod 5K+
P: 5,333
hi ...

avoid to use document.write use innerHTML or standard dom-methods to update node contents or change attributes ...

kind regards
Jan 28 '08 #2

P: 3
hi ...

avoid to use document.write use innerHTML or standard dom-methods to update node contents or change attributes ...

kind regards
Thanks, but I'm sorry, I have no idea what that means. Can you give me a little more detail? What is "innerHTML"? (I'll follow the link and see if I can figure it out myself, but odds are that I can't).

Thanks again.
Jan 28 '08 #3

gits
Expert Mod 5K+
P: 5,333
here is an example:

[HTML]<html>
<script type="text/javascript">
function init_page() {
var node = document.getElementById(container);

node.innerHTML = 'test-content';
}
</script>
<body onload="init_page();">
<div id="container"></div>
</body>
</html>
[/HTML]
here we simply use a div with id 'container' where a function retrieves the reference to it by id and sets innerHTML of it onload of the document. the value to be set has to be a string and could even contain html-markup ...

kind regards
Jan 28 '08 #4

P: 3
here is an example:

[HTML]<html>
<script type="text/javascript">
function init_page() {
var node = document.getElementById(container);

node.innerHTML = 'test-content';
}
</script>
<body onload="init_page();">
<div id="container"></div>
</body>
</html>
[/HTML]
here we simply use a div with id 'container' where a function retrieves the reference to it by id and sets innerHTML of it onload of the document. the value to be set has to be a string and could even contain html-markup ...

kind regards

THANKS, BUT SORRY, DIDN'T WORK. COULD IT BE THAT I'M USING FIREFOX?
Jan 29 '08 #5

gits
Expert Mod 5K+
P: 5,333
sorry my bad ... typo :) fix is here, id has to be a string ofcourse:

Expand|Select|Wrap|Line Numbers
  1. var node = document.getElementById('container');
kind regards
Jan 29 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.