javascript - Jquery : How to prevent prefix addition to address within an `<a>` Tag -


i have html <a> tag follows:

<a id="ext_link" href="www.google.com">google home</a> 

and using jquery open link in new window. jquery below:

$(document).ready(function(){     $("#ext_link").click(function(e){         e.preventdefault();         window.open($(this).attr("href"));     }); }); 

when use <a id="ext_link" href="http"//www.google.com">google home</a> works fine, when use <a id="ext_link" href="www.google.com">google home</a> new window opens , addressbar contains http://localhost/app/www.google.com wrong address obviously...

how can prevent automatic addition of http://localhost/app/ @ beginning.

you can use regex test if href start http:// or https://, if not add http://:

$(document).ready(function(){     $("#ext_link").click(function(e){         e.preventdefault();         var url = $(this).attr("href");         if(!/^(http|https):\/\//.test(url)){             url = "http://" + url;         }         window.open(url);     }); }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -