Javascript 使用在不同页面中创建的变量

Javascript 使用在不同页面中创建的变量,javascript,jquery,html,Javascript,Jquery,Html,my index.html页面中有两个JavaScript变量。我想在control.js中使用这两个变量 html有一些jQuery脚本,如下所示 <script> $(document).ready(function() { // load select code from country.html $('#selectCourse_country').load('country.html select', function() { // when country is

my index.html页面中有两个JavaScript变量。我想在control.js中使用这两个变量

html有一些jQuery脚本,如下所示

<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> 
我想在control.js中使用变量countryId和stateId的值。如何使用jQuery.cookie将它们从index.html传递到control.js。

以下内容应该适合您的需要

要写cookies

$.cookie("countryId", countryId);
$.cookie("stateId", stateId);
然后在control.js中可以得到如下值

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

jQuery解决方案很好。这是一个 使用饼干

为了简洁起见,我只为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
 }
}

这可能对你有帮助

使用modernizer将数据从html传递到另一个js文件

在项目中创建一个可以包含任何对象的全局容器。这些对象可以在应用程序的任何js页面中全局访问

然后在control.js中使用它们,如:


control.js在另一个页面中,还是在同一个页面中?1。将它们保存在DOM2上。将它们保存在DOM数据库中难道你不能在加载control.js之后立即使用jquery将变量存储在隐藏的DOM中吗?许多html页面可以加载相同的javascript文件。没有javascript页面。该文件包含一些代码的文本。该代码的变量仅存在于浏览器上,由javascript代码初始化。共享数据必须通过ajax请求,或者是HTML的一部分,或者在cookie或本地数据库中。cookie如何?您可以使用jQuery.cookie。你能将control.js代码发布到你试图使用这些变量的地方吗?
//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'
]);
window.global ={country_ID : countryId , state_ID : stateId};
var contId =window.global.country_ID;
var statId =window.global.state_ID;