![](/img/trans.png)
[英]How do I get “Hide/Show” and “add marker” functions to work together in Google maps?
[英]How do I get two js functions to work together?
我想使用JS登录网站,然后将URL更改为同一网站上的其他页面。 我有两个职能,他们两个都是独立工作的,但不能一起工作。 我不知道如何编写/组织这些功能,使它们可以一个接一个地执行。
function autoLogin() {
document.getElementById("userName").value = "username";
document.getElementById("pwd").value = "password";
document.getElementById("LoginButton").click();
}
autoLogin();
function nextPage() {
window.location.replace("https://www.website.com/page2");
}
nextPage();
您始终可以在第一个函数中添加回调,以调用传入的第二个函数。例如:
function autoLogin(callback) {
document.getElementById("userName").value = "username";
document.getElementById("pwd").value = "password";
document.getElementById("LoginButton").click();
callback();
}
function nextPage() {
window.location.replace("https://www.website.com/page2");
}
autoLogin(nextPage);
不过,这取决于您的LoginButton单击,同时在后台同步执行某些操作。 如果要等待登录中发生的任何事情先发生,则可能只想调用通过登录按钮调用的函数,而不是实际触发单击。 然后,您可以将回调传递给该函数;
假设您的LoginButton单击调用了一个名为doLogin()的函数。 您可以将回调添加到该函数中,然后在调用之前检查它在被调用时是否存在,以保持向后兼容性。
function doLogin(callback) {
// get username and password and then asynchronously login
if (callback) {
callback();
}
}
function autoLogin(callback) {
document.getElementById("userName").value = "username";
document.getElementById("pwd").value = "password";
doLogin(callback);
}
function nextPage() {
window.location.replace("https://www.website.com/page2");
}
autoLogin(nextPage); //This calls autoLogin, which passes the callback to doLogin.
//Once doLogin is done, it calls nextPage.
我想以下内容将带您进入新页面。
document.getElementById("LoginButton").click();
因此,nextPage()将不会被调用。
也许,对于页面2上加载的脚本,您可以在页面加载时有条件地重定向到page2。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.