lisovaccaro
lisovaccaro

Reputation: 33986

Javascript, redirect to a link retrieved by a function?

Right now I'm showing an href that redirects to a certain location, I want to redirect automatically to the same link but don't know how.

This is the link:

<script>
document.write('<a href="?s=' + geoip_city() + '"> Go </a>');
</script>

I tried with window.location but I'm messing somehow since I go literally to the name of the function.

How should I do it? Thanks

Upvotes: 0

Views: 160

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039180

You should use window.location.href to redirect. So assuming you have a function which returns an url:

function geoip_city() {
    ...
    return 'http://example.com?foo=bar';
}

you could redirect like this:

window.location.href = geoip_city();

Upvotes: 0

FishBasketGordo
FishBasketGordo

Reputation: 23142

Setting window.location.href should redirect you to a new page, like this:

var url = geoip_city(); // url should be a valid url string
window.location.href = url;

Upvotes: 1

Related Questions