Javascript更改位置href并单击某个按钮

时间:2022-06-02 08:57:06

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. This button opens a modal.

我想将用户onclick重定向到某个页面“user / logged_in1”,如果该用户尚未在该页面上,并且在重定向之后必须单击某个按钮。此按钮可打开模态。

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. I need that modal only to be opened if the redirect happens.

这有点复杂。只有在用户使用重定向时才能打开模态。如果我在body标签上使用onload,如果页面加载,模式将始终打开,我不需要。如果重定向发生,我只需要打开模态。

The whole thing is a Python flask application:

整个事情是一个Python烧瓶应用程序:

{% 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. 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听起来像是一个可能的解决方案。但我无法弄清楚如何在单击按钮后删除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");
        }
    } 
});

6 个解决方案

#1


2  

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. Found this method

2:登陆该页面后,您可以检查参数是否存在。找到了这个方法

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://*.com/a/21903119/3514785

在这里找到了方法:https://*.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(); because it is pointless

a:编辑你的页面并删除这一行document.getElementsByClassName(“zimmerbtn”)[0] .click();因为它毫无意义

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. 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.

您可以改为在重定向之前使用localStorage或某些cookie来存储目标ID。确保记住在抓取目标页面后将其清除,以便即使您没有从触发整个过程的页面重定向也不会始终触发它。

#2


1  

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. This sounds like the essential of an anchor tag:

您告诉我们您要将用户重定向到页面onclick。这听起来像锚标记的基本要素:

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

点击我

For step 2, just place the javascript on the page you are redirecting to. Bind to the load event of the page and then execute your javascript:

对于第2步,只需将javascript放在要重定向到的页面上即可。绑定到页面的加载事件,然后执行您的javascript:

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

#3


1  

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. When you done with opening the modal clear the cookie. In that way you can easily achieve this.

当用户单击重定向按钮并设置页面时设置cookie,检查cookie是否已设置并打开模式。完成打开模态后,清除cookie。这样你就可以轻松实现这一目标。

#4


0  

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()">

#5


0  

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
);

#6


0  

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.

在函数中,我检查我是否已经在logged_in站点上。如果不是我重定向,但我在URL中添加了一个额外的参数?重定向。

In the next step if the logged_in is loaded and the parameter is in the URL the modal will also open, thats it.

在下一步中,如果加载了logged_in并且参数在URL中,则模态也将打开,就是这样。

Thanks @Zuks for the Idea adding a param to URL.

感谢@Zuks为Idea添加一个param到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();
         } 
    } 
});

#1


2  

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. Found this method

2:登陆该页面后,您可以检查参数是否存在。找到了这个方法

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://*.com/a/21903119/3514785

在这里找到了方法:https://*.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(); because it is pointless

a:编辑你的页面并删除这一行document.getElementsByClassName(“zimmerbtn”)[0] .click();因为它毫无意义

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. 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.

您可以改为在重定向之前使用localStorage或某些cookie来存储目标ID。确保记住在抓取目标页面后将其清除,以便即使您没有从触发整个过程的页面重定向也不会始终触发它。

#2


1  

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. This sounds like the essential of an anchor tag:

您告诉我们您要将用户重定向到页面onclick。这听起来像锚标记的基本要素:

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

点击我

For step 2, just place the javascript on the page you are redirecting to. Bind to the load event of the page and then execute your javascript:

对于第2步,只需将javascript放在要重定向到的页面上即可。绑定到页面的加载事件,然后执行您的javascript:

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

#3


1  

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. When you done with opening the modal clear the cookie. In that way you can easily achieve this.

当用户单击重定向按钮并设置页面时设置cookie,检查cookie是否已设置并打开模式。完成打开模态后,清除cookie。这样你就可以轻松实现这一目标。

#4


0  

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()">

#5


0  

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
);

#6


0  

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.

在函数中,我检查我是否已经在logged_in站点上。如果不是我重定向,但我在URL中添加了一个额外的参数?重定向。

In the next step if the logged_in is loaded and the parameter is in the URL the modal will also open, thats it.

在下一步中,如果加载了logged_in并且参数在URL中,则模态也将打开,就是这样。

Thanks @Zuks for the Idea adding a param to URL.

感谢@Zuks为Idea添加一个param到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();
         } 
    } 
});