Wasiim
Wasiim

Reputation: 146

How to prepend a https to url on click - jquery

I ran into a situation whereby i have to enable https when a user click on a button.

my current url is as follows: myurl.com

i have to do the following:

$(".btn").click(function(){

// change url to https://myurl.com

});

My current button's html:

<button class="btn" data-statsopt_noninteraction="" data-statsopt_value="" data-statsopt_label="login" data-statsaction="account-actions" data-statscategory="header-events">LOG IN</button>

Any help would much be appreciated

Thanks

Upvotes: 0

Views: 347

Answers (7)

Ankit Gupta
Ankit Gupta

Reputation: 1

$(".mybutton").click(function () {
    url = "www.myurl.com";
    url = 'http://' + url;
});

Upvotes: -1

Gabriele Romanato
Gabriele Romanato

Reputation: 84

Attaching a JS-only action only to a scriptable element (a button in this case ) is a no-no for accessibility. By doing so you actually prevent people who use a non-supporting user-agent from accessing your content. You should add a noscript element:

<button id="js-http-action">HTTPS</button>
<noscript>
   <a href="https://yoururl/">HTTPS</a>
</noscript>

In a textual browser such as Lynx this will be greatly appreciated by users. :-)

Upvotes: 1

Felix
Felix

Reputation: 38102

You can use replace() to replace current protocol http:// with https://:

$(".mybutton").click(function(){
    location.href = location.href.replace("http://", "https://");
});

or better to check if your current URL is https:// or not then redirect the page:

$(".mybutton").click(function(){
    if (location.href.indexOf("https://") == -1) {
        location.href = location.href.replace("http://", "https://");
    }
});    

Upvotes: 2

Jai
Jai

Reputation: 74738

You can do this:

$(".btn").click(function(){
    var url = window.location.href.replace(/http/, 'https');
    window.location.href = url;
});

Upvotes: 0

Ankit Tyagi
Ankit Tyagi

Reputation: 2375

$(".mybutton").click(function(){    
  var str = document.URL     
  location.href = str.replace('http','https');
});

Upvotes: 0

slash197
slash197

Reputation: 9034

$(".mybutton").click(function(){
        window.location.href = "https://myurl.com";
});

Upvotes: 1

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85545

$(".mybutton").click(function(){

// change url to https://myurl.com
window.location.href='https://myurl.com';

});

Upvotes: 0

Related Questions