2012-09-12 67 views
0

我使用的是如下因素代码(位于文件swipe.js):的Javascript访问变量

$(document).ready(function() { 

    $('.ui-slider-handle').live('touchstart', function(){ 
     // When user touches the slider handle, temporarily unbind the page turn handlers 
     doUnbind(); 
    }); 

$('.ui-slider-handle').live('mousedown', function(){ 
    // When user touches the slider handle, temporarily unbind the page turn handlers 
    doUnbind(); 
    }); 

$('.ui-slider-handle').live('touchend', function(){ 
    //When the user let's go of the handle, rebind the controls for page turn 
    // Put in a slight delay so that the rebind does not happen until after the swipe has been triggered 
    setTimeout(function() {doBind();}, 100); 
}); 

$('.ui-slider-handle').live('mouseup', function(){ 
    //When the user let's go of the handle, rebind the controls for page turn 
    // Put in a slight delay so that the rebind does not happen until after the swipe has been triggered 
    setTimeout(function() {doBind();}, 100); 
}); 

// Set the initial window (assuming it will always be #1 
window.now = 1; 

//get an Array of all of the pages and count 
windowMax = $('div[data-role="page"]').length; 

doBind(); 
}); 
// Functions for binding swipe events to named handlers 
function doBind() { 
    $('div[data-role="page"]').live("swipeleft", turnPage); 
    $('div[data-role="page"]').live("swiperight", turnPageBack); 
    } 

function doUnbind() { 
    $('div[data-role="page"]').die("swipeleft", turnPage); 
    $('div[data-role="page"]').die("swiperight", turnPageBack); 
    } 

// Named handlers for binding page turn controls 
function turnPage(){ 
    // Check to see if we are already at the highest numbers page 
    if (window.now < windowMax) { 
    window.now++ 
    $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings")}, false, true); 
} 
else 
      { 
       window.now= window.now-2; 
       $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings")}, false, true); 
} 
} 

function turnPageBack(){ 
    // Check to see if we are already at the lowest numbered page 
    if (window.now != 1) { 
    window.now--; 
    $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings"), reverse: "true"}, true, true); 
} 
else 
      { 
       window.now = window.now+2; 
       $.mobile.changePage("#page"+window.now, {transition: window.localStorage.getItem("transitionsSettings"), reverse: "true"}, true, true); 
} 
} 

我也有另外一个文件名为loadFeed.js。它们都装里面的index.html按这个顺序:

<script type="text/javascript" src="script/swipe.js"></script> 
<script type="text/javascript" src="script/loadFeed.js"></script> 

这里是loadFeed.js文件:

google.load("feeds", "1"); 
alert(window.now); 
switch(now) 
{ 
case 1: 
    var link = "http://www.zive.sk/rss/sc-47/default.aspx"; 
    var listviewID = "feedZive"; 
    break; 
case 2: 
    var link = "http://mobilmania.azet.sk/rss/sc-47/default.aspx"; 
    var listviewID = "feedMobil"; 
    break; 
case 3: 
    var link = "http://www.automoto.sk/rss"; 
    var listviewID = "feedAuto"; 
    break; 
} 
alert(link); 
alert(listviewID); 
alert("#"+listviewID); 

function initialize() { 
    var feed = new google.feeds.Feed(link); 
    feed.setNumEntries(window.localStorage.getItem("entriesNumber")); 
    feed.load(function(result) { 
     if (!result.error) { 
      var feedlist = document.getElementById(listviewID); 
      for (var i = 0; i < result.feed.entries.length; i++) { 
       var li = document.createElement("li"); 
       var entry = result.feed.entries[i]; 
       var A = document.createElement("A"); 
       A.setAttribute("href",entry.link); 
       A.appendChild(document.createTextNode(entry.title)); 
       li.appendChild(A); 
       feedlist.appendChild(li); 
      } 
      $("#"+listviewID).listview("refresh"); 
     } 
    }); 
} 
google.setOnLoadCallback(initialize); 

是否有任何可能的方式来存取权限在loadFeed.js window.now变量?

+0

是;只需访问它。窗口引用在页面上的所有脚本中都是全局的。 – Pointy

+0

试图把alert(window.now)放在loadFeed.js里,但是我得到了undefined。 – horin

+2

请注意,您在'$(document).ready'中设置了'window.now'。所以你需要等待另一个文件,直到'ready'已经发生并且变量已经被设置(即,把另一个引用'now'放入一个ready函数中)。 – pimvdb

回答

0

是的。 window对象随处可用。这是全球范围变量的麦克爸爸。您可以在“loadFeed.js”或任何JavaScript块或文件中使用window.now

对于您的情况,您可以在文档就绪回调中将now属性添加到window对象。所以这不会立即发生。你不应该期望window.now马上就有价值。我会建议之一:

1)包装中的代码,在一个文档准备回调访问window.now像很多的swipe.js代码 “loadFeed.js”:

$(document).ready(function() { 
     //your code 
    }); 

OR

2)在swipe.js中,给window.now一个在doc就绪回调之外的值。

+0

我可以在文档之外给它一个值,但我认为它只会改变insinde doc ready回调,但我需要它在回调之外进行更改。 – horin

+0

我很难理解你的评论...只需添加'$(document).ready(function(){alert(window.now);});'loadFeed.js,你会看到我是什么谈论。 – MicronXD

+0

我已经做到了这一点,我已经通过警报获得了值,所以它正在工作,但是当我点击确定提醒时,我的页面看起来像正在加载但加载永远不会结束som我的Feed从未加载:) – horin