Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Good way to write text into a JS page

Status
Not open for further replies.

darkling235

Programmer
Oct 30, 2006
24
US
Can anyone suggest a good way to write text into a JS page?
Basically I'm using an IFRAME to show dynamic feedback to the user.
I need to be able to write text into this separate JS page and remove it later.

I.E. The user clicks a button
The words "You clicked a button!" appears in my IFRAME

The user clicks the other button
The words are deleted.

Can anyone tell me how to do that or tell me where to look to discover it?
Thanks a lot.
 
Why would the content need to be a separate page loaded in an iframe?
Could you just put a DIV on the existing page and write the info into it? You could use the innerHTML property of the DIV to change it's content and not have to deal with an iframe.


At my age I still learn something new every day, but I forget two others.
 
Thanks for responding. The spec of this project says we need the output to be in a separate panel (this is for a mozilla extension so my boss thinks having a separate panel will make it look nicer).

In any case I need to be able to write information there. Now getting back and forth between the main page and an IFRAME isn't too complicated just like sending information back and forth between any other frames

But I still need a way to write information to a page. I'm a bit of a Javascript newb.
 
An iframe would be a section on the existing page that loads a seperate page rather than another browser window.

If you need to dynamically alter the content of the page then you could do it more easily by using a DIV on the page and writing the info directly.
To do it in an iframe you have to first write the changes to the file on the server then tell the iframe to load the new page and you just are not going to be able to do that with javascript. Javascript is client-side code and only affects things on the client's PC, not on the server.
The one exception to this is if you are running an IIS server and have it configured to use server-side JScript.

Again, if your intention is to have the dynamic content in the same web browser screen as the rest of your content then you are better off using a DIV on that page for the content. Then you do not have to create new pages and load them, you just write the content directly into that section of the page.

If you MUST use iframes for some reason then you need server-side code to create the pages.


At my age I still learn something new every day, but I forget two others.
 
Thank you for explaning that. Is there anyway I can use Javascript to keep the display in the same page but make it look like a panel of a separate page? Just to keep it looking nice and separate?


Also at the risk of beating a dead horse what function can I use to write stuff to the page itself? I mean if I want new text to appear in the page how can I do it? I'm very new to javascript.
Thanks a lot.
 
You can style it any way you want.
If you were using an iframe how would you make it look seperate?

You can put the code in a DIV and set the position of the div absolute. This makes the div stay fixed in that position on the page even if it were on top of any other page content. You can style the div so it has borders around it or use a table within it that has borders, etc.

Give the div an id tag like this:
<div id="myDiv">My content in here</div>

From your javascript code you get a reference to the div by it's ID tag:
var myDivObj = document.getElementById('myDiv');

Then you can change properties on that div through the myDivObj you declared. To change the HTML within the div you would do:
myDivObj.innerHTML = 'Some new content for the div';

And then the content is changed to the new content. VERY easy to do.

First, setup a test page for yourself where you can change the content in the div so you see how it works. Then work on using position absolue (google absolute positioning) to see how you can place your div on the page.

I use exactly this technique all the time. Rather than using popup windows to do things like popup calendars. Rather than a popup I create a DIV for the calendar but set it's visibility to none. When I need the calendar I change the DIV's visibility so it shows and it is set absolute on top of the rest of the page. When done with the calendar I turn it's visibility off again.



At my age I still learn something new every day, but I forget two others.
 
Thank You. That helps a lot. It also seems much simpler than the IFRAME. I'll work on that implementation
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top