简体   繁体   English

如何检查您是否在jQuery Mobile的特定页面上

[英]how to check if you are on a certain page in jquery mobile

I would like to know if it is possible if test which page you are on using jquery mobile and based on that page do whatever you need to. 我想知道是否有可能测试使用jquery mobile并基于该页面的页面上您需要做的事情。 Here is the code I was using: 这是我正在使用的代码:

if(window.location.href == '#searchpage')
{
    $("#searchtitle").val($(this).attr('titleResult'));
    $("#searchauthor").val($(this).attr('authorResult'));
    $("#searchpublisher").val($(this).attr('publisherResult'));
    $( "#searchedition" ).val($(this).attr('editionResult'));
    $("#searchDetailsSearchResults").empty();
}

ie if I am on the page called searchpage then we would fill in text boxes with certain values. 即,如果我在名为searchpage的页面上,那么我们将在文本框中填充某些值。 As such I'm struggling to find a way to determine if I am on a given page? 因此,我正在努力寻找一种方法来确定我是否在给定页面上? How would I achieve this? 我将如何实现?

Fetch current url using pure JS: var current_url = window.location.pathname; 使用纯JS获取当前网址: var current_url = window.location.pathname;

Fetch current url using jQuery: var current_url = $(location).attr('href'); 使用jQuery获取当前网址: var current_url = $(location).attr('href');

If you're looking for the page title instead of url: var current_title = $(document).attr('title'); 如果要查找页面标题而不是URL: var current_title = $(document).attr('title');


Then you would simply use that to check if you're on the expected page: 然后,您只需使用它来检查您是否在预期的页面上:

var current_title = $(document).attr('title');
if (current_title == "Home") {
    console.log("on the homepage");
}

您正在寻找$.mobile.activePage

if $.mobile.activePage.attr("id") == "searchpage" // do stuff

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

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