简体   繁体   English

JQuery Mobile:在新页面上运行 javascript 方法

[英]JQuery Mobile: Run javascript method on new page

I have an app that allows users to win prizes.我有一个应用程序可以让用户赢取奖品。 To win a prize they need to get to a page like this: www.myUrl.com#isAWinner where #isAWinner is my mobile page.要赢得奖品,他们需要访问这样的页面: www.myUrl.com#isAWinner其中#isAWinner是我的移动页面。 I am worried that someone will think of just entering that url and going directly to the winner page without actually winning.我担心有人会想到只是输入那个 url 并直接进入获胜者页面而没有真正获胜。 To fix this I attempted to do this:为了解决这个问题,我尝试这样做:

<!-- Show this if the user is a winner -->
<div id = "isAWinner" data-role = "page">
    <p>YOU WIN!!!</p>
    <!-- Invisible link -->
    <a href = "#beforeLogin" id = "goBack" class = "InvisibleLinks"></a>
    <!-- Ensures that the user does not just enter #isAWinner to the end of the URL -->
    <script type="text/javascript"> reallyAWinner()</script>
</div>

function reallyAWinner () {
    alert(isAWinner);
    //Check to see if they really are a winner
    if (isAWinner == false) {
        //Not really a winner. Send back to login
        $('#goBack').click();
    }
}

The problem is that the alert is hit when the page initially loads, but if i try to go to that URL afterwords, then the method is not hit again.问题是在页面最初加载时会触发alert ,但是如果我尝试将 go 转换为 URL 后记,则不会再次触发该方法。

Am I not understanding how JQuery mobile works?我不了解 JQuery 移动设备的工作原理吗? Will it only hit that reallyAWinner() method when the whole HTML page loads, and not when the isAWinner page loads?它只会在整个 HTML 页面加载时才命中该reallyAWinner()方法,而不是在isAWinner页面加载时? If so, is there another way I can check if the user is really a winner only when the isAWinner page loads?如果是这样,是否有另一种方法可以仅在加载isAWinner页面时检查用户是否真的是赢家?

Edit : here is a little more info.编辑:这里有更多信息。 When I enter the method initially without loading the first page, the alert in reallyAWinner() will fire before an alert I have in my document.ready method, making the $('#goBack').click();当我最初在没有加载第一页的情况下进入该方法时, reallyAWinner()中的警报将在我的document.ready方法中的警报之前触发,从而使$('#goBack').click(); not work (Because mobile is not loaded)不工作(因为手机没有加载)

If you have enabled ajax method calls the function reallyAWinner() will be called when your initial page is called.如果您启用了 ajax 方法调用 function 将在调用初始页面时调用真正的AWinner()。

What you can do to call you function reallyAWinner() only when #isAWinner page is loaded you can register a "pageshow" event on the id of your page.只有在加载#isAWinner 页面时,您才能调用 function realAWinner() 您可以在页面的 id 上注册“pageshow”事件。

So you can do the following:因此,您可以执行以下操作:

$('#isAWinner').live('pageshow', function () { reallyAWinner();  });

From jQueryMobile Documentation:来自 jQueryMobile 文档:

pageshow: Triggered on the page being shown, after its transition completes.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM