Jump to content
Sign in to follow this  
Mudsaf

jQuery variable passing from <script> to another <script>

Recommended Posts

Hello, i'm wondering is it possible to pass variable inside <script></script> to another <script></script> field.

 

Example i have

<script>var message = "Hello";</script><script>alert(message);</script>

I know i could add them together in this script i write, but my actual codes is separating the scripts.

Share this post


Link to post
Share on other sites

All scripts by default run in the same scope, the window scope, so functions and variables that are defined in that scope are available for any code running in that scope.

Share this post


Link to post
Share on other sites

Well seems like my variable wont pass.

 

Uncaught ReferenceError: json_list is not defined

<script>$(function() {var json_list;$.getJSON('<myfile.js>', function(data) {json_list = data;});});</script>/* Later */<script>$(function() {	console.log(json_list);});</script>
Edited by Mudsaf

Share this post


Link to post
Share on other sites

You defined that variable as local inside another function, that's not the window scope. If you want to use it in the function and make sure you're using the global version, use window.json_list.

Share this post


Link to post
Share on other sites

or just var json_list, outside of any function() {} blocks, similar to this example from your other thread

http://w3schools.invisionzone.com/index.php?showtopic=48172#entry266827

Edited by thescientist

Share this post


Link to post
Share on other sites
<script>$(function() {var json_list;$.getJSON('../script/json/selectbox.js', function(data) {window.json_list = data;console.log(window.json_list); //Returns undefinedconsole.log(data); //Returns JSON array});});</script>

Share this post


Link to post
Share on other sites

You're still defining that variable locally, but I don't get the same results. If you define and run this function:

 

function test_func() { window.test_var = 'test'; }

 

Then outside that function, in the window scope, both test_var and window.test_var are set to "test".

Share this post


Link to post
Share on other sites

Ajax solved everything xD

var json_list;$.ajax({    url: "<myfile.js>",    dataType: 'json',    async: false,    success: function(data) {        json_list = data;    }});

Share this post


Link to post
Share on other sites

 

Ajax solved everything xD

 

no it didn't, you made a global variable like we were saying.

 

 

 

or just var json_list, outside of any function() {} blocks, similar to this example from your other thread

http://w3schools.invisionzone.com/index.php?showtopic=48172#entry266827

 

 

The lesson to learn here is how variable scope works in javascript. It's something any decent javascript developer should make it a point to learn.

http://msdn.microsof...a(v=vs.94).aspx

Edited by thescientist

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...