JQuery Mobile:在新页面上运行 javascript 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6294446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery Mobile: Run javascript method on new page
提问by Stanley Cup Phil
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. I am worried that someone will think of just entering that url and going directly to the winner page without actually winning. To fix this I attempted to do this:
我有一个允许用户赢取奖品的应用程序。要赢得奖品,他们需要进入这样的页面:我的移动页面www.myUrl.com#isAWinner
在哪里#isAWinner
。我担心有人会想到只是输入该网址并直接进入获胜者页面而没有真正获胜。为了解决这个问题,我试图这样做:
<!-- 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
当页面最初加载时被命中,但如果我尝试转到该 URL 后缀,则该方法不会再次被命中。
Am I not understanding how JQuery mobile works? Will it only hit that reallyAWinner()
method when the whole HTML page loads, and not when the isAWinner
page loads? If so, is there another way I can check if the user is really a winner only when the isAWinner
page loads?
我不明白 JQuery 移动是如何工作的吗?它只会reallyAWinner()
在整个 HTML 页面加载时调用该方法,而不是在isAWinner
页面加载时调用吗?如果是这样,是否有另一种方法可以仅在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();
not work (Because mobile is not loaded)
编辑:这里有更多信息。当我最初在没有加载第一页的情况下进入该方法时,警报reallyAWinner()
将在我的document.ready
方法中有警报之前触发,从而$('#goBack').click();
无法正常工作(因为未加载移动设备)
回答by ppolyzos
If you have enabled ajax method calls the function reallyAWinner() will be called when your initial page is called.
如果您启用了 ajax 方法调用,则在调用初始页面时将调用函数 realAWinner()。
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 页面时,您才能调用真正的AWinner() 函数,您可以在页面的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.