tags:

views:

21

answers:

3

i have this index.html

    <script type="text/javascript" src="js/jquery-latest.js"></script>
    <script type="text/javascript" src="jqFunc.js"></script>
    <script type="text/javascript">

           var kamote = 6;

    </script>

and this jqFunc.js

$(function(){
   alert(kamote);
});

the problem is the value 6 wont appear.. what is way to call variables from other file like that?

A: 

You have to put kamote in the global window object.

Try:

<script type="text/javascript" src="js/jquery-latest.js"></script>
<script type="text/javascript" src="jqFunc.js"></script>
<script type="text/javascript">
    window.kamote = 6;
</script>

and this jqFunc.js

$(function(){
    alert(window.kamote);
});
Day
`var kamote = 6;` outside a function will do the same thing.
Strelok
@Strelok Good point, I should have thought a bit and tested before jumping in. My first downvote ;)
Day
Actually the OP's code works for me when I test it...
Day
A: 

javascript is an interpreter. It will read your codes line by line and then execute it. You missed the logic there. You have assigned a value to a variable after alerting it. Which is not the thing you expected.

try this,

<script type="text/javascript">
   var kamote = 6;
</script>
<script type="text/javascript" src="jqFunc.js"></script>

and see what I mean. ;)

added notes:

but still you might get the desired output at some time. It is because you have wrapped your alert with the ready handler event. If your DOM takes long to be ready, alert will be 6. But if it's quick, you will not get 6.

Reigel
+2  A: 

I have tested , its working fine

<script type="text/javascript"  src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript" src="jqFunc.js"></script>
<script type="text/javascript">
 var kamote = 6;
</script>

There might be a problem with

<script type="text/javascript" src="js/jquery-latest.js"></script>

ie js/jquery-latest.js file dont exits!

JapanPro
I have an explanation on my answer about that. ;)
Reigel
@Reigel ya was trying to response that on your post. that shifting dont make sense here.
JapanPro
is this posible? $(document).ready(function(){ $.post('settings.php?retVar=slideSpeed', function(e){ var x = e; }); alert(x);}); i got no response from x but if i replace var x = e with alert(e), i works fine
vrynxzent
@vrynxzent try this $(document).ready(function(){ var x; $.post('settings.php?retVar=slideSpeed', function(e){ x = e; }); alert(x);});
JapanPro
@JapanPro - but still, your assumption is still debatable. IMHO, it's just because of the ready handler. Shifting codes is a sure go but, yes, not necessary at some point. Try not shifting the codes, but remove the ready handler if it still alert 6 ;)
Reigel
@JapanPro: it returns undefineds.. i think the x=e has been limit to function(e) only.. it will not return a value to var x..
vrynxzent
@vrynxzent the reason why i have kept var x on top , so you can assign, check response from php is correct.
JapanPro
@JapanPro.. it says undefined.. how about this sir? $(document).ready(function() { $.post('settings.php?retVar=slideSpeed', function(e){ var x = e; }); alert(x);});
vrynxzent
@vrynxzent how about this $(document).ready(function(){ $.post('settings.php?retVar=slideSpeed', function(e){ alert(e); }); });
JapanPro