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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

document.writeln prints on a new page 2

Status
Not open for further replies.

shmmeee

Programmer
Apr 17, 2001
104
GB
I've got a script that, when a form is submitted it checks for a description that is written in a text area and a URL that is displayed as a link underneath but when the link is printed it replaces everything on the screen rather than just printing below the text area. Here's a bit of my code:

function get_defn(term_string)
{
window.onerror = undefined_term;
defn_string = eval(term_string.replace(/ /g,"_").toUpperCase());

defn_array = defn_string.split("=");
return defn_array[0]
};
function write_link()
{
document.writeln(&quot;<a href='&quot; + defn_array[1] + &quot;'>Link</a>);
}

//the functions are called like so:
<input type=&quot;button&quot; name=&quot;B1&quot; value=&quot;Search!&quot; onclick=&quot;javascript:document.form1.output.value = get_defn(document.form1.searchtext.value); write_link();&quot;>

Please help.
 
document.write always just replaces what is on the screen. You need to use either layers or spans. You could set up a span where you want the new code to display. Inside the span initially just have &nbsp;. Then in your Javascript function just change the HTML code using something like:

function write_link()
{
mySpan.innerHTML = '<a href=&quot;' + defn_array[1] + '&quot;>Link</a>'
}

Where mySpan is the ID of your span.

Hope this helps.

Mise Le Meas,

Mighty :)
 
hi!

quote:

When you call the write( ) method, it opens and clears the document if the document is not in the process of being opened and parsed when the write( ) method is called, so it can be dangerous.

that is why, for IE:
use
obj.innerHTML+=string_to_insert
or
obj.insertAdjacentHTML(where_to,string_to_insert)

for NC:
i'm not shure, but try to open document before writing anything to it (smth like this):
document.open()
document.writeln('blabla')
document.close()

it might help you...

 
oops!
mighty did it first!
i'm too slow! (just few seconds, i swear!!) ;)

best regards,vic
 
Thanks Mighty (& vic),
it works perfectly. Will that work in netscape as well? if not what do I have to do?

Iain X-)
 
shmmeee,

I'm not sure if it will work in Netscape. Mise Le Meas,

Mighty :)
 
hi!

i've checked, my previous suppose

quote:

for NC:
i'm not shure, but try to open document before writing anything to it (smth like this):
document.open()
document.writeln('blabla')
document.close()


doesnt work: it clears the document's content

well, u might try to use layer with a unique ID & write to this layer; the entire document wont suffer (but i think it wuld clear layer's content)
(use this refer:)
document.layers.layerID.document.write('bla')
u may open this layer before inserting & close after it (that's kinda right way)
 
note: if u won't close that layer
(won't write
document.layerID.document.open() before &
document.layerID.document.close() after)

than your *FIRST* (if u are to do this operation more than one time) string will clear the layerID content, but the other's won't



best regards, vic
 
vic,
I'm not sure I understand your last post. I want to replace what's there each time I check for a URL, like the <span> solution does on IE. Does that mean I do want to use open and close?

Iain X-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top