Welcome to WebmasterWorld Guest from 54.242.83.7

Forum Moderators: open

Message Too Old, No Replies

Problem with javascript in netscape

     
10:42 am on Jul 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Ive got a javascript in my head section which does some work based on data pulled out of a cgi script. I am trying to place this into the webpage with another javascript at the insertion point which uses a function call to get the data. So basically the script in the head takes some variables and joins them all up in a document.write. This is then called from the other script. This works fine in IE but Netscape seems to have a problem with the document.write being in a separate script. Anyone know how to overcome this?

Cheers

10:55 am on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 10, 2001
posts:793
votes: 0


Can you post the code?
11:02 am on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Hi Joshie,

It goes like this:

This is the script in the head:

function displayBanner(){
var imageURL=image;
var linkURL=link;
writeImage(linkURL, imageURL);
}

This is the one in the body:

displayBanner();
function writeImage (linkURL, imageURL){
document.write('<a href="'+linkURL+'"><img src="'+imageURL+'" border="0" width="125" height="125">');

}

Cheers

11:16 am on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 10, 2001
posts:793
votes: 0


Is
displayBanner()
called before the
writeImage()
function is written in the source?

Do you know what the error is? (type javascript: into the address bar just after the error occured).

edit>>DOH! I can see that your displayBanner() function is called before the writeImage function exists and think this may be the cause of your problems...
can you try:

function writeImage()
{
...
}
displayBanner();

11:22 am on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


I tried that but thats not it. I think my function names may be a little misleading. The displaybanner function assigns values to the variables based on values taken from the cgi. The writeImage function takes these values and concatenates them into a hyperlinked img. It works great for IE but netscape doesnt output anything, and also doesnt suggest there are any errors. Confusing huh!!
11:30 am on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 27, 2002
posts:959
votes: 0


Is there a reason you have this in 2 different functions and not something like this?

function displayBanner(){
var imageURL=image;
var linkURL=link;
document.write('<a href="'+linkURL+'"><img src="'+imageURL+'" border="0" width="125" height="125">');
}

11:34 am on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Hi Sinner, thanks for chipping in. Funny you should say that, thats what I started with, but I wondered if that could be the problem so I changed it - but it still didnt work.

Cheers

12:05 pm on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 27, 2002
posts:959
votes: 0


Robber, I just did a local test page with just the one function. Just had the image and link URLs directly in the function and not from a cgi. And it worked perfectly well (NS 4.7 and NS7.0 preview), so maybe the problem is in you cgi?
12:10 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Thanks for the test sinner - I think you might be right. I just did a similar test except pulled the data out of a diferent cgi and it seemed to work fine. Seems to be reading another part of the cgi when in Netscape even though I've got an exit statement in their - but I guess thats another problem now - I best bury my head in a book.

Cheers

12:37 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Does anyone know if Netscape has a problem with putting a cgi as the src of a javascript?

edit>>
Just in case anyone is following this - netscape seemed to have a problem with the cgi script. The cgi was outputting under a javascript mime header as part of a subroutine but netscape was trying to read the whole cgi for some reason (no idea why). So I put that sub into its on cgi doc and it works fine now.

12:58 pm on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 10, 2001
posts:793
votes: 0


Netscape would have no way of knowing the JS came from CGI, the CGI is just delivering *normal but dynamic* text source to the browser.

Maybe you could show us some of the generated source (with the CGI output)or stickymail me the URL.

1:11 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Im just trying to upload it to a public server so you can take a look - trouble is, its broken in IE now!! I'll let you know when we're up and running.

Cheers

1:34 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


I think I know what the problem is, but I dont really know why I get it. In the cgi I have a part that checks the referer, this is both for security and to tell the script what to do depending on who the referer is. I think that in netscape this part is breaking down so the sub with the javascript stuff in it isnt executing - hence why I get my variable being undefined.

Does Netscape have a problem with HTTP_REFERER?

2:31 pm on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 10, 2001
posts:793
votes: 0


Personally I wouldn't rely on the HTTP_REFERER as, just looking in my logs I see total nonsense all the time.

I don't know but maybe NS4 doesn't pass the referer to file in the <script src="cgi" ... />, perhaps a QueryString could solve your problem, eg:

<script src="cgi-bin/myfile.pl?mode=js" ... />

2:49 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Thats an idea, I guess the only problem is that people could manipulate the script easier then. I had figured that if I used the HTTP_REFERER then I could verify that all requests to the cgi were from a page on my site. Perhaps its not the best way, I'll give your suggestion a go.

edit>>> Oh, and BTW, its not just NS4, NS^ does exactly the same. I tried just printing the referer and its empty for both.

Cheers

3:04 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


Finally, I have cracked what the problem is - and it seems so simple now. I think Netscape only tracks the HTTP_REFERER if someone actually clicks a link - not if the url is say an img or javascript src. Well that explains it. Just tested this with a simple text link and the HTTP_REFERER is tracked in IE and NS so that would appear to be the problem.

Hope someone finds that useful!

Cheers

5:16 pm on July 18, 2002 (gmt 0)

Senior Member

WebmasterWorld Senior Member tedster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:May 26, 2000
posts:37301
votes: 0


That's very useful. It may explain some long-standing strangeness for me. Time to go digging.

Thanks!

5:52 pm on July 18, 2002 (gmt 0)

Preferred Member

10+ Year Member

joined:May 15, 2001
posts:462
votes: 0


No problem tedster, Im glad all my problems are of some use. You never know, I'll probably post another one tomorrow!!

Cheers