Closed Thread Icon

Topic awaiting preservation: Checking for a variable existence, dummy problem (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=8966" title="Pages that link to Topic awaiting preservation: Checking for a variable existence, dummy problem (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: Checking for a variable existence, dummy problem <span class="small">(Page 1 of 1)</span>\

 
Gauthier
Obsessive-Compulsive (I) Inmate

From:
Insane since: Dec 2003

posted posted 12-09-2003 16:48

Hi there!

I'm having a problem with javascript only when I run them in separate .js files, and not when I'm using raw script in my HTML.

Here is a short exemple of what will work:

code:
<html>
<head>
<title></title>
</head>
<body>
<script>
var A = {
Function : function()
{
if(B)
alert(B);
else
alert('B NOT FOUND');
}
}
A.Function();
var B = {}
</script>
</body>
</html>



Now I want to run the same A.Function() call, but I need to move A and B singletones objects definition in separate js file, I got something like this:

code:
<html>
<head>
<title></title>
</head>
<body>
<script src="test1.js"></script>
<script src="test2.js"></script>
<script>
A.Function();
</script>
</body>
</html>



where test1.js and test2.js contains respectively declaration for A and B singletones, that still work because both objects are declared, now remove the js file that contain B and you will obtain 'B' is undefined error wich is sad because I was just checking B existence in my A code.

So if anyone have a solution to solve this issue nicely (I need to use separate js files for my singletones objects declarations), I would be very glad to hear it!

Thanks a billion.

Gauthier

Gauthier
Obsessive-Compulsive (I) Inmate

From:
Insane since: Dec 2003

posted posted 12-09-2003 17:06

Doh!!

Since I've writed thanks a billion and that always after taking the time to formulate a problem to explain it that a solution came to mind!

I simply put my sensible code in a try catch block as a clean solution, why didn't I got this thought before????

But if anyone have an explanation why the bug is encountering, I'm always open for it

Thanks (and sorry)

Gauthier

biu
Neurotic (0) Inmate
Newly admitted

From:
Insane since: Dec 2003

posted posted 12-15-2003 12:35

if(typeof B != 'undefined')
alert(B);
else
alert('B NOT FOUND');


....

if(window['B'])
alert(B);
else
alert('B NOT FOUND');

ciao ciao

« BackwardsOnwards »

Show Forum Drop Down Menu