Forum Moderators: open

Message Too Old, No Replies

Checking if a certain function exists on the page

         

Dabrowski

11:25 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have <some> JS's included in my HTML with <script src....> tags.

Is there a way of making them aware of each other? Checking if a certain function exists on the page for example?

JAB Creations

11:32 pm on Apr 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So you're looking to check for a function from within a function? What is the common condition you're requiring for this script ultimately?

- John

cmarshall

12:18 am on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Should definitely be able to do it.

When you include JavaScript into a page, whether by an "src" or by a direct implementation, the page becomes the scope and the context. Everything is on that page.

I depend on that for that AJAX library I PMed you.

There is the problem of not being able to access or use JS until it has been added to the page.

For example:

<html><head><title>Spaz</title></head><body><script type="text/javascript">var spaz='spaz';</script><script type="text/javascript">alert(spaz);</script></body></html>

Will display "spaz," but:

<html><head><title>Spaz</title></head><body><script type="text/javascript">alert(spaz);</script><script type="text/javascript">var spaz='spaz';</script></body></html>

Will give an error.

JS is a bit weird about being invoked as the page loads. I have gotten inconsistent results.

Dabrowski

1:04 am on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah I hold everything back until onLoad fires.

I tried in one script:

function myThing() {
stuff
}

and then in the other:

if( myThing) {
more stuff
}

But it didn't seem to work. Got a myThing undefined error. Should it work? I'll check the loading order thing and the onload handler again if it should.

cmarshall

2:35 am on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check for format errors.

If you have a format error before the function definition, then the function will never be defined.

Also, it's good practice to test with: if(typeof(myThing)!='undefined'){};

Dabrowski

10:48 am on Apr 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for that, I'll give that method a try. That way it won't throw an error if the function is NOT defined.