最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript: How to use setAttribute "href" for a URL refering to another domain - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 8

That'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://'

发布评论

评论列表(0)

  1. 暂无评论