【发布时间】:2014-03-13 06:13:33
【问题描述】:
我有一个 HTML <a> 标签如下:
<a id="ext_link" href="www.google.com">GOOGLE HOME</a>
我正在使用 JQUERY 在新窗口中打开链接。 JQUERY如下:
$(document).ready(function(){
$("#ext_link").click(function(e){
e.preventDefault();
window.open($(this).attr("href"));
});
});
当我使用<a id="ext_link" href="http"//www.google.com">GOOGLE HOME</a> 时它工作正常,但是当我使用<a id="ext_link" href="www.google.com">GOOGLE HOME</a> 时会打开一个新窗口并且地址栏包含http://localhost/app/www.google.com 这显然是错误的地址...
如何防止在开头自动添加http://localhost/app/。
【问题讨论】:
-
您必须提供
http://(或任何协议),否则将被解释为相对链接。 -
在href中提供网址时,您应该提供完整的网址,包括
httpothttps -
如果不添加则检查字符串是否以
http(s)?://开头。 -
试试
href="//www.google.com"
标签: javascript php jquery html