# Хочу обновить rel=canonical href с помощью JavaScript

**URL:** <https://meta.discourse.org/t/i-want-to-update-rel-canonical-href-using-java-script/159527>\
**Category:** Product Support\
**Created:** [01.Август.2020 16:00:23 UTC](https://meta.discourse.org/t/i-want-to-update-rel-canonical-href-using-java-script/159527 "2020-08-01T16:00:23Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![neounix](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/neounix/32/215617_2.png) [@neounix](https://meta.discourse.org/u/neounix)\
**Post date:** [02.Август.2020 04:58:34 UTC](https://meta.discourse.org/t/i-want-to-update-rel-canonical-href-using-java-script/159527/2 "2020-08-02T04:58:34Z")

</div>

> [@KranthiKiranGude](#):
>
> Может кто-нибудь подсказать, как обновить тег href с помощью JavaScript в Discourse?

Вот вам, @KranthiKiranGude, вот как можно изменить атрибут `href` в JavaScript. Сначала вы выбираете элемент DOM, затем изменяете атрибут.

```html
<script>
var uC = document.querySelectorAll("link[rel='canonical']")[0];
var newURL = "https://my.coolforum.com/newlink";
uC.setAttribute("href", newURL);
</script>

```

Конечно, вам понадобится какая-то логика в зависимости от страницы, на которой вы хотите работать.

Пример общей логики:

```html
<script>
if("the_actual_page_url_or_id" == "my_interesting_page_url_or_id")
{
   var uC = document.querySelectorAll("link[rel='canonical']")[0];
   var newURL = "https://my.coolforum.com/newlink";
   uC.setAttribute("href", newURL);
}
</script>

```

Надеюсь, это поможет.

---

_[View the full topic](https://meta.discourse.org/t/i-want-to-update-rel-canonical-href-using-java-script/159527)._
