Javascript opening new window with external URL (Django)

2k views Asked by At

I have some simple lines of code:

that.click(function(){
    window.open($('.linkBox input').val());       
});

Assuming I'm redirecting to google.com, whenever a new window is opened, the URL is: "my/project/url/http://www.google.com"

Basically whatever URL is inputted, it gets appended to the end of my project's URL. How can I avoid this?

1

There are 1 answers

0
Muthu Kumaran On BEST ANSWER

I think the problem could be the missing http:// in the URL, try this code

that.click(function(){        
        var url = $('.linkBox input').val();
        if (!/^https?:\/\//i.test(url)) {
            url = 'http://' + url;
        }
        window.open(url);
});

Working Demo: http://jsfiddle.net/muthkum/BPBev/1/