1

I have defined on external JavaSsript file which contains one variable such as the following

var demo='Hello'". 

I want to use this variable's value in jQuery's $(document).ready() event. How can I accomplish this?

1
  • Any thing u put inside document.ready will become private. So if u want to access some variable inside document, it has to be outside document.ready Btw. document.ready can simplified $(function({ }); Commented Aug 1, 2013 at 7:55

2 Answers 2

2

All you have to do is put the external file above your main script:

<script src="external.js"></script>
<script>
     // "demo" is accessible here
</script>

Script tags load synchronously, so once the second script tag executes, you can be sure the first one has already loaded.

Edit

Per the comment, this script works for me:

<html>
    <script src="global.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-2.0.3.js"></script> 
    <script type="text/javascript"> 
        $(document).ready(function() { var x=abc(); alert('hello'+x); }); 
    </script>
</html>

Where global.js is just:

function abc() { var valu='hello'; return valu; }
Sign up to request clarification or add additional context in comments.

2 Comments

Hi McGarnagle.This is my script :==<script type="text/javascript" src="global.js"></script> <script type="text/javascript" src="../../Script Library/jquery-1.4.3.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var x=abc(); alert('hello'+x); }); </script> ==This is my externla .js file code:==function abc() { var valu='hello'; return valu; }:But this is not working.please help me
@Naveen I think there must be some other problem -- your code works for me (see edit above). Do you see any script errors?
0

If you have defined that file before the file with the ready event, it should be available providing the scope is correct. ie:

<script type="text/javascript" src="scriptwithvalue.js"></script>
<script type="text/javascript" src="scriptthatwantsthevalue.js"></script>

When I'm passing variables between files, I tend to wrap the logic up in a getter and setter, ie:

File One

var name = "Dave";

function getName()
{
    return name;
}

File Two

var name = getName();

Edit

As above, if your Javascript is already in the HTML file, then simply importing the script with the value in it is enough.

4 Comments

does it really make sense? I guess just importing external js will do for it
This was assuming that the user is practising unobtrusive javascript
Hi Chris.This is my script :==<script type="text/javascript" src="global.js"></script> <script type="text/javascript" src="../../Script Library/jquery-1.4.3.min.js"></script> <script type="text/javascript"> $(document).ready(function() { var x=abc(); alert('hello'+x); }); </script> ==This is my externla .js file code:==function abc() { var valu='hello'; return valu; }:But this is not working.please help me
Good lord, edit it into your question please. And are you still having the same problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.