简体   繁体   English

获取当前的DOM元素

[英]Get current DOM element

I have a functioning site that includes a search function that loads results into a <div> element via a jQuery $.ajax call like so: 我有一个正常运行的站点,其中包括一个搜索功能,该功能通过jQuery $ .ajax调用将结果加载到<div>元素中,如下所示:

$.ajax({
    url:        '{{ path('order_search') }}',
    type:       $(this).attr('method'),
    data:       $('#search_form').serialize(),
    success:    function (data) {
        $('#js-search-results').html(data);
    }
});

I'm working in a twig template within a symfony project, thus the url notation. 我正在一个symfony项目中的树枝模板中工作,因此使用url表示法。 This is working perfectly. 这很正常。

This is on a site that requires login. 这是在需要登录的站点上。 I have an event listener in symfony that checks for a period of inactivity at each each kernel request (taken from here ), and if the inactive period exceeds a maxIdleTime then the user is redirected to the login page. 我在symfony中有一个事件侦听器,它在每个内核请求(从此处获取 )中检查不活动的时间段,如果不活动的时间段超过maxIdleTime,则将用户重定向到登录页面。

My problem is that if the user is inactive for a period and then enters a search, the js-search-results div is filled with the login page. 我的问题是,如果用户在一段时间内处于非活动状态,然后输入搜索,则js-search-results div会充满登录页面。 What I would like to happen is to have the entire window redirect to the login page. 我想发生的是将整个窗口重定向到登录页面。 Seems simple, but I haven't figured out how to do it. 看起来很简单,但是我还没有弄清楚该怎么做。

One what I thought to handle would for my login page script to check whether it was being loaded into the full window (rather than just a div element), and if not then refresh the entire window. 我认为要处理的一个问题是,我的登录页面脚本将检查它是否已加载到整个窗口中(而不只是div元素中),如果不是,则刷新整个窗口。 How to do this? 这个怎么做?

It sounds like you want to find out what type of data is there and if it is the login page do a redirect. 听起来好像您想找出其中的数据类型,如果是登录页面,请进行重定向。

Find out if the data contains elements that are in the login page. 找出data包含登录页面中的元素。 if it is do a window.location.href = "/login" if not display the search results in the div. 如果是,则执行window.location.href = "/login"如果未在div中显示搜索结果)。 send data from backend like { page : "login"} 从后端发送数据,例如{ page : "login"}

You can check the content inside the variable " data " to check if it contains elements from the login page and then use an if else statement. 您可以检查变量“ data ”内的内容,以检查其是否包含登录页面中的元素,然后使用if else语句。

if(data.includes("username") && data.includes("password")){
  window.location.assign("https://your.login.page.html");
}else{
  $('#js-search-results').html(data);
}

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

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