1

There are two JavaScript variables in my index.html page. I want to use these two variables in control.js.

index.html has some jQuery script like below

<script>
$(document).ready(function() {
// load select code from country.html
$('#selectCourse_country').load('country.html select', function() {
    // when country is selected
    $('#selectCourse_country select').change(function() {
        // get id
        var countryId = $(this).children('option:selected').val();
        // load select code from state.html by id
        $('#selectCourse_state').load('state.html #'+countryId,function(){
            $('#selectCourse_state select').change(function(){
                var stateId = $(this).children('option:selected').val();
               //alert(stateId);                    
            });
        });
    });
  });
});
</script> 

I want to use value of variables countryId and stateId in control.js. How can I pass them from index.html to control.js.

6
  • Is control.js in another page, or the same one? Commented Jul 15, 2013 at 10:18
  • 1. save them on the DOM 2. save them on the DOM db Commented Jul 15, 2013 at 10:18
  • Couldn't you just load control.js right afterwards and store variables in a hidden dom with jquery? Commented Jul 15, 2013 at 10:18
  • Many html pages can load the same javascript file. There are no javascript pages. The file contains the text of some code. The variables for that code only exists on the browser, as initialized by the javascript code. Shared data has to be requested with ajax, or be part of the HTML, or in a cookie or local database. Commented Jul 15, 2013 at 10:22
  • how about cookies? you can use jQuery.cookie. Can you post the control.js code where you are trying to use those vars? Commented Jul 15, 2013 at 10:23

4 Answers 4

2

Make a global container in your project which can contain any object. and those objects can be access globally in any js page of your application.

window.global ={country_ID : countryId , state_ID : stateId};

Then use them in control.js like :

var contId =window.global.country_ID;
var statId =window.global.state_ID;
Sign up to request clarification or add additional context in comments.

Comments

0

Using jQuery.cookie (https://github.com/carhartl/jquery-cookie) the following should suit your needs.

To write the cookies...

$.cookie("countryId", countryId);
$.cookie("stateId", stateId);

and then in control.js you can get the values like this...

var countryId = $.cookie("countryId");
var stateId = $.cookie("stateId");

Comments

0

The jQuery solution is good. This is a Use cookies.

for brevity I only coded for stateID

//Store cookie.
 var stateId = $(this).children('option:selected').val();
 document.cookie = "stateid="+ stateId;

//Get user cookie and match stateID
var cookies = document.cookie.split(';')
for (var i = 0 ; i< cookies.length;i++){
 if(cookies[i].match(/stateid/)){
 //Do stuff
 }
}

Comments

0

This may be helpful for you.

Passing data from html to another js file using modernizer.

//Load JS through Modernizer
Modernizr.load([
  // Functional polyfills
  {
    // This just has to be truthy
    test : Modernizr.websockets && window.JSON,
    // socket-io.js and json2.js

    // Arrays of resources to load.
    both : [ '/js/jquery.min.js', '/js/jquery-ui-min.js'],
    complete : function () {
        // Run this after everything in this group has downloaded
        // and executed, as well everything in all previous groups
        $('#selectCourse_country').load('country.html select', function() {
        // when country is selected
        $('#selectCourse_country select').change(function() {
        // get id
        var countryId = $(this).children('option:selected').val();
        // load select code from state.html by id
        $('#selectCourse_state').load('state.html #'+countryId,function(){
            $('#selectCourse_state select').change(function(){
            var stateId = $(this).children('option:selected').val();
               //alert(stateId);                    
            });
        });
        });
      });
    }
  },
  // Run your code afte you've already kicked off all the rest
  // of your app.
  // Pass variables to this js file
  '/js/control.js'
]);

Comments

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.