简体   繁体   English

Javascript更改位置href,然后单击某个按钮

[英]Javascript change location href and click on a certain button

I would like to redirect the user onclick to a certain page "user/logged_in1" if the user is not already on that page and after the redirect a certain button must be clicked. 我想将用户onclick重定向到某个页面“ user / logged_in1”,如果该用户尚未在该页面上,并且在重定向之后必须单击某个按钮。 This button opens a modal. 此按钮将打开模式。

Doesn't work with this function: 不适用于此功能:

function redirect_to_logged_in_and_open_modal() {
    if(window.location.href.indexOf("logged_in") > -1) {
        return
     } else {
         location.href="user/logged_in1";
         document.getElementsByClassName("zimmerbtn")[0].click();    
     }
}

It seems it searched already for the button before the redirect happens and therefore the list is empty. 似乎在重定向发生之前已经在搜索按钮,因此列表为空。 How to fix this? 如何解决这个问题?

EDIT 编辑

This is a little bit more complex. 这有点复杂。 The modal should only open if the user uses the redirect. 仅当用户使用重定向时,模式才应打开。 If I use onload on the body tag, the modal will always open if the page is loaded, I don't need that. 如果我在body标签上使用onload ,则在加载页面时,模式将始终打开,我不需要。 I need that modal only to be opened if the redirect happens. 我仅在重定向发生时才需要打开该模式。

The whole thing is a Python flask application: 整个过程是一个Python flask应用程序:

{% if current_user.zahlung_iban == None and have_this_user_a_room != None %}
        <li><a class="btn mybtn" onclick="redirect_to_logged_in_and_open_modal()" data-toggle="modal" data-target="#premium-special-modal"> Zimmer anbieten </a></li>
{% else %}
        <li><a class="btn mybtn" href="{{ url_for('zimmer_einstellen') }}"> Zimmer anbieten </a></li>
{% endif %}

As you see there is a certain trigger for the button to become the redirect button. 如您所见,按钮具有一定的触发作用,成为重定向按钮。

EDIT 编辑

Okay working with a cookie sounds like a possible solution. 好的,使用cookie听起来像是一种可能的解决方案。 But I cant figure out how to delete the cookie after the button was clicked, none of the proposed code works, eventhough it looks simple: 但是我无法弄清楚如何在单击按钮后删除cookie,尽管看起来很简单,但是建议的代码都无效:

function redirect_to_logged_in_and_open_modal() {
    if(window.location.href.indexOf("logged_in") > -1) {
        return
     } else {
         location.href="user/logged_in1";
         document.cookie = "redirected_coz_of_click";
     }
}

$( document ).ready(function() {
    if ($(".logged-in-container")[0]) {

        var delete_cookie = function(name) {
            document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
        };

        if (document.cookie.indexOf('redirected_coz_of_click') > -1 ) {
            document.getElementsByClassName("zimmerbtn")[0].click();
            delete_cookie('redirected_coz_of_click');
            console.log(document.cookie);
        } else {
            console.log("cookie removed");
        }
    } 
});

1: Append a parameter to the redirect url 1:将参数附加到重定向网址

user/logged_in1?click=btn_id

2: On landing on that page you can then check if the parameters is there. 2:在该页面上,您可以检查参数是否存在。 Found this method 找到了这个方法

var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = decodeURIComponent(window.location.search.substring(1)),
    sURLVariables = sPageURL.split('&'),
    sParameterName,
    i;

    for (i = 0; i < sURLVariables.length; i++) {
        sParameterName = sURLVariables[i].split('=');

        if (sParameterName[0] === sParam) {
            return sParameterName[1] === undefined ? true : sParameterName[1];
        }
    }
};

Found the method here: https://stackoverflow.com/a/21903119/3514785 在这里找到方法: https : //stackoverflow.com/a/21903119/3514785

So you could use it like this: 因此,您可以像这样使用它:

var btnToClickId = getUrlParameter("click");

3: With jquery (or javascript if you want) fire the click event on that btn: 3:使用jquery(或JavaScript(如果需要))在该btn上触发click事件:

$("#"+btnToClickId).click();

NB: You want to do the check after the page has loaded: 注意:要在页面加载后进行检查:

$(window).on("load",function() {
    var btnToClickId = getUrlParameter("click")
    if(btnToClickId) {
        $("#"+btnToClickId).click();
    }
});

So in summary 所以总结

a: Edit your page and remove this line document.getElementsByClassName("zimmerbtn")[0].click(); a:编辑页面并删除此行document.getElementsByClassName("zimmerbtn")[0].click(); because it is pointless 因为没有意义

b: In the target page in the js copy and past the `getUrlParameter' method. b:在js副本的目标页面中,并通过`getUrlParameter'方法。

c: Then inside a on window load event listener do the url check as specified in 3. c:然后在一个on window加载事件监听器中,按照3中指定的方式进行url检查。

ALTERNATIVE TO URL PARAMETER 替代URL参数

You could instead use localStorage or some cookie to store the target id right before redirecting. 您可以改为在重定向之前使用localStorage或某些Cookie存储目标ID。 Make sure you remember to clear it in the target page after grabbing it so that it is not always triggered even when you have not redirected from the page that triggers this whole process. 确保记住在抓取目标之后将其清除在目标页面中,以便即使您未从触发整个过程的页面重定向时也不会始终触发该目标。

You can not access elements that have not even loaded yet. 您无法访问尚未加载的元素。 By using location.href = '' you are simply directing a user to a page, any javascript after will fail because the page hasn't been loaded yet. 通过使用location.href ='',您只是将用户定向到页面,之后的所有javascript都将失败,因为该页面尚未加载。

You tell us that you want to redirect a user to a page onclick. 您告诉我们您要将用户重定向到页面onclick。 This sounds like the essential of an anchor tag: 这听起来像锚标记的本质:

<a href="user/logged_in1">Click Me</a>

For step 2, just place the javascript on the page you are redirecting to. 对于步骤2,只需将javascript放在您要重定向到的页面上。 Bind to the load event of the page and then execute your javascript: 绑定到页面的加载事件,然后执行您的javascript:

window.onload = function() {
    document.getElementsByClassName("zimmerbtn")[0].click();  
};

You can use cookie . 您可以使用cookie

Set cookie when user click on the redirect button and when page is loaded, check if the cookie is set and open the modal. 当用户单击重定向按钮并加载页面时设置cookie,请检查cookie是否已设置并打开模式。 When you done with opening the modal clear the cookie. 打开模式后,清除cookie。 In that way you can easily achieve this. 这样,您可以轻松实现这一目标。

Instead of triggering click. 而不是触发点击。 Call the onclick function in body onload method of user/logged_in1 . user / logged_in1的 body onload方法中调用onclick函数。

<body onload="openModal()">

You are trying to access a content on your initial page, but you have already redirected the user from that page, so this cannot be achieved by the current method. 您正在尝试访问初始页面上的内容,但是您已经从该页面上重定向了用户,因此当前方法无法实现。

I believe what you are trying to do is to open up a modal, after redirecting, do the following. 我相信您想要做的是打开一个模式,重定向后,执行以下操作。

On your redirected page add the following, use jquery to achieve this 在您的重定向页面上添加以下内容,使用jquery实现此目的

$( document ).ready(
   // add the functionality to launch modal here
);

I created an onclick() function for this button: 我为此按钮创建了一个onclick()函数:

onclick="redirect_to_logged_in_and_open_modal()"

In the function I check if I am not already on the logged_in site. 在该函数中,我检查我是否还没有登录网站。 If not I redirect but I add an additional parameter to the URL ?redirected . 如果不是,我会重定向,但是会向URL ?redirected添加一个附加参数。

In the next step if the logged_in is loaded and the parameter is in the URL the modal will also open, thats it. 在下一步中,如果已加载logging_in并且参数在URL中,则模式也会打开,仅此而已。

Thanks @Zuks for the Idea adding a param to URL. 感谢@Zuks的想法为URL添加了参数。

function redirect_to_logged_in_and_open_modal() {
    if(window.location.href.indexOf("logged_in") > -1) {
        return
     } else {
         window.location.assign("https://www.example.com/user/logged_in1?redirected");
     }
}

$( document ).ready(function() {
    if ($(".logged-in-container")[0]) {

        if(window.location.href.indexOf("redirected") > -1) {
            document.getElementsByClassName("zimmerbtn")[0].click();
         } 
    } 
});

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

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