PDA

View Full Version : Dynamically write JavaScript inbetween JS tags


mark wills
10-30-2002, 04:42 PM
Hi there, I have some code that looks like this:

<script language="JavaScript" id="Imsg"></script>
<script language="JavaScript">
CheckMsg();
function CheckMsg()
{ Imsg.src='check_msg.asp';
setTimeout("CheckMsg()",20000);
}
</script>

This defines an empty Javascript tag, called Imsg.
The CheckMsg() function calls an active server pages page, which will either write some javascript back between the empty tags (defined by Imsg) or not, depending on if there is a message waiting for the user or not. This is then called every 20 seconds.

It works perfectly in IE 5 & 6, but I have tried the site in Netscape 7.0 and it doesn't work. Nothing happens.

I am wondering how I can make it Netscape compatible. Any ideas?

Nothing fancy happens in check_msg.asp - it just does a response.write("alert('check your messages');") if a message is waiting - thus this gets written in between the empty java script tags and executed. As I say, perfect in IE, but dead in NS. I'd appreciate some comments. I'm new to Netscape (although the GUI looks much better than IE!) It's as if Netscape can't resolve 'Imsg.src'

Mark.

Roy Sinclair
10-30-2002, 09:26 PM
Try this:

<script language="JavaScript"><!--#include file "'check_msg.asp"--></script>

mark wills
10-30-2002, 10:03 PM
Hi Roy - thanks for the reply...

The problem with #include is that the content that is included is statically fixed when the page is rendered. The javascript I have below (in my earlier post) is different in that a small peice of java script that runs every 20 seconds calls an ASP page, the output of which is 'squirted' into the empty <script></script> tags, thus executed by the browser.

Thus, every 20 seconds, a back end database is checked by check_msg.asp, and if a message is found for the logged on user the messge is written in the ASP page with Response.Write - that output is placed inbetween the <script></tags> and viola - a message pops up on the screen (the ASP page just writes an alert(); function).

So, the problem seems to be with this line:

Imsg.src='check_msg.asp';

In IE, all output from check_msg.asp is injected between the empty <script></script> tags (id=Imsg) - however this isn't working in netscape. Netscape obviously requires a different syntax to place the output into the script tags.... I'll be damned if I can find out what it is though :(

Mark.

Roy Sinclair
10-30-2002, 10:45 PM
Mark,

Since the object is to get an field on the page that updates every x seconds why not write the contents to an IFRAME instead?

realisis
10-31-2002, 05:14 PM
Mark,

I did some experimenting on this about a month ago. NS 6+ has both a .src property and a .text property for scripts. However, at the time I wasn't concentrating on using an ID, but you can try.

document.getElementById('Imsg').src

if that doesn't work, here's what I used

document.getElementsByTagName('script')[0].src

replacing the number in brackets with the script number in question.

Can't find my notes right now, but I do seem to remember NS grabbing the new src...

...

If you want to achieve the same in NS4, you have to write the actual script tag to a layer via document.write() method on the layer contents.

...

Let me know if this works for you?

(EDIT: corrected a typo ...)