简体   繁体   English

如何单击按钮使用JavaScript转到某些页面?

[英]how to click the button the go to some page with javascript?

Is it possible to this? 这可能吗?

http://url/jsp/index.jsp?id=xxx&password=xxx

I will include it in button, so when user click the button they go to some page also login with the id and password. 我将其包含在按钮中,因此,当用户单击按钮时,他们会转到某个页面并使用ID和密码登录。

First: Putting an id and password in the page (whether in a link, in variables in code, or whatever) is a really bad idea . 第一:将idpassword放在页面中(无论是在链接中,在代码中的变量中还是在任何其他内容中)都是一个不好的主意 I strongly recommend you don't do that. 我强烈建议您不要这样做。


Assuming you really want to pass the id and password as query string parameters, yes it's possible : 假设您真的想将idpassword作为查询字符串参数传递, 可以的

<input type="button" onclick="location = 'http://url/jsp/index.jsp?id=xxx&password=xxx'" value="Login">

...but...this is what links are for. ...但是...这是链接的用途。

<a href="http://url/jsp/index.jsp?id=xxx&password=xxx">Login</a>

(You can style the link to look like a button if you want.) (如果需要,可以将链接设置为看起来像按钮的样式。)


If your goal is to go to that URL where access to the resource is protected by authentication (eg, the browser would pop up a window asking for the username and password), then it depends on what kind of authentication is being used. 如果您的目标是转到通过身份验证保护对资源的访问的URL(例如,浏览器将弹出一个窗口,询问用户名和密码),则取决于所使用的身份验证类型。 For username and password ones, then you can, you just format the link differently: 对于用户名和密码,则可以用不同的格式设置链接:

http://username:password@url/jsp/index.jsp

(Not anymore, thankfully. As Jeremy Miller points out below, neither IE nor Chrome supports this (any more), and if the others haven't already followed suit, I wouldn't be surprised if they did at some point...) (不再是,谢天谢地。正如Jeremy Miller在下面指出的那样, IEChrome都不支持此功能(如果再有),并且如果其他人还没有效仿的话,如果他们在某个时候这样做,我不会感到惊讶... )

But again, it's a really bad idea . 但这又是一个非常糟糕的主意

It is possible by this solution. 通过这种解决方案是可能的。

<button id="buttonID" link="your login url">login</button>

$(document).ready(function() {
    $('#buttonID').click(function() { 
        var link = $(this).attr("link");

        if(link ) {
            window.location = link;
        }
    });
});

Assign the id to your button and replace "buttonID" with your actual button id. 将ID分配给您的按钮,然后将“ buttonID”替换为您的实际按钮ID。 In click event do the above. 在点击事件中,请执行上述操作。 The url must call the login function (controller function if it is MVC) which should accepts these parameters, do authentication and redirects to the landing page. 网址必须调用登录功能(如果是MVC,则为控制器功能),该功能应接受这些参数,进行身份验证并重定向到登录页面。

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

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