Rave Sean
Rave Sean

Reputation: 131

How to get href value/url from a javascript function and redirect to it?

I am trying to redirect to a hostname which is coming as part of request after appending https:// to this.

<a target="_blank" href="javascript:createDynamicPubUrl();" >

Below is the function to create a url by appending hostname with protocol(https) :

function createDynamicPubUrl() {
     publisherHostName = document.getElementById('hostname').value;
     var pubUrl ;
     var protocol = 'https://';
     pubUrl = protocol+publisherHostName;
     return pubUrl;
}

Instead of redirecting to pubUrl it is printing the pubUrl on the webpage.

Upvotes: 1

Views: 87

Answers (2)

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

You could redirect using window.location.href :

<a target="_blank" href="javascript:window.location.href = createDynamicPubUrl()" >link</a>

Hope this helps.

Upvotes: 1

JVDL
JVDL

Reputation: 1202

Redirecting to HTTPS is better done on the server side. If however you want to redirect to the HTTPS version on the client side, you can use window.location.href:

function createDynamicPubUrl() {
    publisherHostName = document.getElementById('hostname').value;
    var pubUrl ;
    var protocol = 'https://';
    pubUrl = protocol+publisherHostName;
    return pubUrl;
}

window.location.href = createDynamicPubUrl();

Upvotes: 0

Related Questions