[英]location.href to a link without http://
I have a webpage - http://www.example.com/ - where I have a button. 我有一个网页 - http://www.example.com/ - 我有一个按钮。 On click of the button I need to go to a particular link (www.google.com).
点击按钮,我需要转到特定链接(www.google.com)。 The button is like :
按钮如下:
<input type="button"
onclick="javascript:location.href='www.google.com'"
value="Click" />
But this button opens a page "http://www.example.com/www.google.com" which is wrong URL. 但是,此按钮会打开一个页面“http://www.example.com/www.google.com”,这是一个错误的网址。
I have tried window.location
, document.location
, document.location.href
, location.href
but all in vain. 我已经尝试过
window.location
, document.location
, document.location.href
, location.href
但都是徒劳的。
The URL in my onclick can not be restricted to begin with 'http://'. 我的onclick中的URL不能限制为以“http://”开头。
您可以使用“当前”协议为URL添加前缀//
Since you did not include a protocol (such as for example http), your browser will interpret www.google.com
as a link to http://www.example.com/www.google.com
, since you are currently on http://www.example.com
. 由于您未包含协议(例如http),因此您的浏览器会将
www.google.com
解释为指向http://www.example.com/www.google.com
的链接,因为您目前使用的是http://www.example.com
。 Add whatever protocol you want to the href-string, but if you are referring to somewhere else than on the site itself, you must have the protocol. 将您想要的任何协议添加到href-string中,但如果您指的是除站点本身之外的其他位置,则必须具有该协议。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.