Forum Moderators: open

Message Too Old, No Replies

Function below call?

         

web_young

9:40 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



This is probably a dumb question and will show my lack of understanding. But is it ok to put a javascript function in the HTML code below where you actually call the function (hope that's the right terminology). For example I have this:

<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
ShowRandomBannerAd();
//-->
</SCRIPT>

above this in the HTML code:

<SCRIPT LANGUAGE=JAVASCRIPT>
<!--
function ShowRandomBannerAd(){
blah blah blah
//-->
</SCRIPT>

The reason I have to do it this way is because of certain restrictions by a third party software. Would that work or not?

kaled

1:59 am on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The issue is whether the function is known when it is called. Functions can appear in any order, BUT, a function must have been declared when it is called.

In the example you give, it would fail. However the following would be ok.

function fna()
{ fnb() }

function fnb()
{ alert('yippee') }

fna();

This would be ok because when fna() is called, both functions are known.

Kaled.

Bernard Marx

9:07 am on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's not quite true (if I understand correctly). There is a little more flexibility with functions.

This also works, even though the declarations of both functions ar below the call.


[b]
fna();

function fna(){ fnb() }

function fnb(){ alert('yippee') }
[/b]

There are two necessary conditions for this:

1. The function must be declared with this kind of syntax (the 'usual' kind)
- function functionName(){ ... }

2. The function definition must be in the same script block as the call (or one above, natch)

#2 Is why web_young's code will cause an error.

Bernard Marx

9:49 am on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few ways round this.

1. Simply copy the function to the top block, where it can be executed. It will be overriden by the 'original' later.


<script>
ShowRandomBannerAd();
function ShowRandomBannerAd(){
blah blah blah
</script>

<script>
function ShowRandomBannerAd(){
blah blah blah
</script>

or 2. Estimate a reasonable code-download margin, and put it into a timer.
In the top block..

setTimeout('ShowRandomBannerAd()',1000)

or 3. That's leaving things to chance a little. You could go all the way, and have a loop that tests for the function, and executes it when found. This way the function will be executed sooner, and there's no chance of attempting to execute it before it exists.


<script>
waitForFn("testFunction",200)

function waitForFn(fnName, delay)
{
var intv = setInterval(repeat, delay)
function repeat(){
if(window[fnName]){ window[fnName](); clearInterval(intv); }
}
}
</script>

<script>
function testFunction(){
alert('hello')
}
</script>

kaled

1:13 pm on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, I'm sure you're right. Whole script blocks are parsed before any execution takes place.

Pascal is my first language, so I am in the habit of declaring all functions before they are called so I've never had cause to test this in detail.

Kaled.

Bernard Marx

1:14 pm on Nov 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds like a good habit anyway.

web_young

3:08 pm on Nov 23, 2004 (gmt 0)

10+ Year Member



Thanks for the help. I tried this method.

1. Simply copy the function to the top block, where it can be executed. It will be overriden by the 'original' later.

<script>
ShowRandomBannerAd();
function ShowRandomBannerAd(){
blah blah blah
</script>

<script>
function ShowRandomBannerAd(){
blah blah blah
</script>

But it doesn't seem like the first function is being overriden by the second function. Should the second function override the first? Thanks again for your help with this.

Bernard Marx

3:21 pm on Nov 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The function call in the first block will call the function as defined in that block.
Any call made to the function after the code in the second block has been parsed will call the second function.

So I guess:

<script>
ShowRandomBannerAd(); // alerts: 1
function ShowRandomBannerAd(){
alert(1)
}
</script>

<script>
function ShowRandomBannerAd(){
alert(2)
}
// below function defn
ShowRandomBannerAd(); // alerts: 2
</script>

If not, try the timer.

web_young

7:10 pm on Nov 23, 2004 (gmt 0)

10+ Year Member



OK, I tried the timer and when the timer expires it goes to a new page and just displays the random banner. I must be doing something wrong, what would cause that to happen?

Bernard Marx

9:05 am on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The ShowRandomBannerAd function itself is such a big unknown in this that it's really hard to say much for certain.

web_young

3:04 pm on Nov 24, 2004 (gmt 0)

10+ Year Member



Here's the actual function. The PRINT/NEWLINE stuff is part of the 3rd part software that is causing all of this headache in the first place

PRINT "<script language='JavaScript1.2'>"
NEWLINE
PRINT "<!--"
NEWLINE
PRINT "function ShowRandomBannerAd(){"
NEWLINE
PRINT " var BannerCount=4;"
NEWLINE
PRINT "<!-- Change BannCount to -1 to turn banners off --!>"
NEWLINE

PRINT " var RandomNumber=Math.floor(Math.random()*BannerCount)+1;"
NEWLINE
PRINT " if (RandomNumber==1){"
NEWLINE
PRINT " ShowBannerAd('ad_billpay2.swf','ad_billpay.jpg',475,60,'billpayer/index.htm');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " else if (RandomNumber==2){"
NEWLINE
PRINT " ShowBannerAd('ad_loanapp2.swf','ad_loanapp.jpg',475,60,'loanapplication/index.htm');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " else if (RandomNumber==3){"
NEWLINE
PRINT " ShowBannerAd('ad_visacard.swf','ad_visacard.gif',475,60,'visa/credit.htm');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " else if (RandomNumber==4)"
NEWLINE
PRINT " {"
NEWLINE
PRINT " ShowBannerAd('ad_estatement.swf','treebanner.jpg',450,58,'services/unet.htm#estatements');"
NEWLINE
PRINT " }"
NEWLINE
PRINT "}"
NEWLINE
PRINT "//-->"
NEWLINE
PRINT "</SCRIPT>"

Bernard Marx

6:44 pm on Nov 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That leads to the question "What's in ShowBannerAd".

I still see no reason why the page should only show the banner.

// Dull code they've written, BTW. Haven't they heard of switches or objects?

web_young

4:13 pm on Nov 29, 2004 (gmt 0)

10+ Year Member



Here's the other function-

PRINT "function ShowBannerAd(MovieName,ImageName,AdWidth,AdHeight,DestURL){"
NEWLINE
PRINT " if (FlashSupported && MovieName!=''){"
NEWLINE
PRINT " document.writeln('<TABLE CELLPADDING=0 CELLSPACING=0 BORDER=0>'+"
NEWLINE
PRINT " '<TR VALIGN=BOTTOM><TD>');"
NEWLINE
PRINT " document.writeln("
NEWLINE
PRINT " '<OBJECT CLASSID='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'');"
NEWLINE

PRINT " if (!(navigator.userAgent.toLowerCase().indexOf('msie 6.0')!=-1))"
NEWLINE
PRINT " document.writeln(' CODEBASE='http://download.macromedia.com/pub'+"
NEWLINE
PRINT " '/shockwave/cabs/flash/swflash.cab#5,0,0,0'');"
NEWLINE
PRINT " else"
NEWLINE
PRINT " document.writeln(' CODEBASE=''');"
NEWLINE
PRINT " document.writeln(' WIDTH='+AdWidth+' HEIGHT='+AdHeight+' ALIGN=TOP>');"
NEWLINE
PRINT " document.writeln(' <PARAM NAME='SRC' VALUE='/ns-icons/'+MovieName+''>');"
NEWLINE
PRINT " document.writeln(' <PARAM NAME='QUALITY' VALUE='high'>');"
NEWLINE
PRINT " document.writeln(' <PARAM NAME='LOOP' VALUE='TRUE'>');"
NEWLINE
PRINT " document.writeln(' <EMBED SRC='/ns-icons/'+MovieName+"
NEWLINE
PRINT " '' PLUGINSPAGE='https://www.macromedia.com/shockwave/download/'');"
NEWLINE
PRINT " document.writeln(' TYPE='application/x-shockwave-flash' WIDTH='+"
NEWLINE
PRINT " AdWidth+' HEIGHT='+AdHeight+' QUALITY='high'');"
NEWLINE
PRINT " document.writeln(' ALIGN='TOP' LOOP='TRUE' VSPACE=0>');"
NEWLINE
PRINT " document.writeln(' </EMBED>');"
NEWLINE
PRINT " document.writeln('</OBJECT>');"
NEWLINE
PRINT " if (DestURL!=''){"
NEWLINE
PRINT " document.writeln('</TD><TD valign=middle>'+"
NEWLINE
PRINT " '<A HREF=''+DestURL+''><IMG SRC='/ns-icons/learnmore.gif'');"
NEWLINE
PRINT " document.writeln(' WIDTH=84 HEIGHT=19 BORDER=0 HSPACE=8 VSPACE=2></A>');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " document.writeln('</TD></TR></TABLE>');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " else if (!(navigator.appName &&"
NEWLINE
PRINT " navigator.appName.indexOf('Netscape')>=0 &&"
NEWLINE
PRINT " navigator.appVersion.indexOf('2.')>=0) &&"
NEWLINE
PRINT " ImageName!=''){"
NEWLINE
PRINT " if (DestURL!=''){"
NEWLINE
PRINT " document.write('<A HREF=''+DestURL+''>');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " document.write('<IMG SRC='/ns-icons/'+ImageName+'' WIDTH='+AdWidth+"
NEWLINE
PRINT " ' HEIGHT='+AdHeight+' BORDER=0>');"
NEWLINE
PRINT " if (DestURL!=''){"
NEWLINE
PRINT " document.write('</A>');"
NEWLINE
PRINT " }"
NEWLINE
PRINT " }"
NEWLINE
PRINT "}"
NEWLINE

Thanks for all your help!

Bernard Marx

3:53 pm on Nov 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure I can help that much.
It might be easier without the PRINT and NEWLINE, especially as this code is generating another level of print-type statements (doc.write).

But maybe it's a good thing you've included this after all. I have no idea whence came the PRINT and NEWLINE - what language it is, or what it's instructing. Some kind of cgi perhaps? Anyways, surely, lines like this below must generate an error or "not what's intended"...

PRINT " document.writeln(' <PARAM NAME='SRC' VALUE='/ns-icons/'+MovieName+''>');" 

That translates to an output of:

document.writeln(' <PARAM NAME='SRC' VALUE='/ns-icons/'+MovieName+''>');

..which is, quotewise, up the Creek without a paddle.