简体   繁体   中英

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.

First: Putting an id and password in the page (whether in a link, in variables in code, or whatever) is a really bad idea . 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 :

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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