简体   繁体   中英

Javascript opening new window with external URL (Django)

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?

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/

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