admin管理员组文章数量:1357639
In my html code I am using an <a>
tag with empty <href>
as the url is not known at the beginning.
Let's say a new aim now is getting known as "www.amazon.de". Now I try to change the href with javascript:
document.getElementById('linkname').setAttribute("href", "www.amazon.de");
But my browser creates only a relative link to my domain like this:
.amazon.de>
(this is shown if i hover over the link)
In my html code I am using an <a>
tag with empty <href>
as the url is not known at the beginning.
Let's say a new aim now is getting known as "www.amazon.de". Now I try to change the href with javascript:
document.getElementById('linkname').setAttribute("href", "www.amazon.de");
But my browser creates only a relative link to my domain like this:
https://www.mydomain.de/www.amazon.de>
(this is shown if i hover over the link)
Share Improve this question edited Mar 8, 2019 at 14:11 Vinz243 9,95611 gold badges45 silver badges95 bronze badges asked Mar 8, 2019 at 14:09 Hubert LohmaierHubert Lohmaier 134 bronze badges3 Answers
Reset to default 8That's because www.amazon.de
is a relative link. To link a different domain (host), you need to begin the authority ponent with double slashes: //www.amazon.de
, or with the scheme included https://www.amazon.de
.
You have to add the protocol ('https://') to the link:
document.getElementById('linkname').setAttribute("href", "https://www.amazon.de");
It seems you are missing 'http://' or 'https://'
本文标签: Javascript How to use setAttribute quothrefquot for a URL refering to another domainStack Overflow
版权声明:本文标题:Javascript: How to use setAttribute "href" for a URL refering to another domain - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744064032a2584656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论