# Button-\>template-node-\>link to another site

**URL:** <https://discourse.nodered.org/t/button-template-node-link-to-another-site/74748>\
**Category:** Dashboard\
**Created:** [5 February 2023 19:52 UTC](https://discourse.nodered.org/t/button-template-node-link-to-another-site/74748 "2023-02-05T19:52:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)\
**Post date:** [5 February 2023 19:52 UTC](https://discourse.nodered.org/t/button-template-node-link-to-another-site/74748/1 "2023-02-05T19:52:21Z")

</div>

Pulling my hair out here, help appreciated.

I have a Node-red with a dashboard site.  
The url used here is not real, but it has the same structure.

Site: [http://weather.com:1880/ui](http://weather.com:1880/ui)

In this site I wish to have a button that triggers a site change:  
[http://weather.com:9000](http://weather.com:9000)

The button triggers a template node (the blue one, not the orange one).  
I am not able to make the site swap to work. So I am doing some tests.  
Sending the url to a debug node also.

Test 1:

```auto
<script>
  (function(scope) {
    scope.$watch('msg.payload', function(data) {
      let origin = window.location.origin
      scope.send({url: origin})
      window.location.href = origin
    });
})(scope);
</script>

```

Result in degub:  
url: "[http://weather.com:1880](http://weather.com:1880)"  
And user is sent to the Node-red editor. This is not what I want.  
So lets try hostname.

Test 2

```auto
<script>
  (function(scope) {
    scope.$watch('msg.payload', function(data) {
      let hostname = window.location.hostname
      scope.send({url: hostname})
      window.location.href = hostname
    });
})(scope);
</script>

```

Result in debug:  
url: "[weather.com](http://weather.com)"  
But user is sent to "[weather.com:1880/ui/weather.com](http://weather.com:1880/ui/weather.com)"  
With error info: Cannot GET /ui/weather.com

What is happening?

Best regards  
Steffen

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 February 2023 21:38 UTC](https://discourse.nodered.org/t/button-template-node-link-to-another-site/74748/2 "2023-02-05T21:38:25Z")

</div>

Can you export your flow ad paste it in a responce.

---

<div class="post-metadata">

**Author:** ![stgj](https://avatars.discourse-cdn.com/v4/letter/s/e495f1/32.png) [@stgj](https://discourse.nodered.org/u/stgj)\
**Post date:** [7 February 2023 15:41 UTC](https://discourse.nodered.org/t/button-template-node-link-to-another-site/74748/3 "2023-02-07T15:41:47Z")

</div>

It looks like I fixed it.

This is apparently not viable:

```auto
const url = window.location.hostname
window.location.href = url

```

The difference between hostname and origin is that origin have the protocol (http:) at first, hostname has not.  
When you link it to another site without the protocol it appends the url at the end of the same url.  
So you get [someurl.com/someurl.com](http://someurl.com/someurl.com)

The problem with origin is that you also get the TCP port (eg :6000).  
This I did not want since I wanted to link to another site with another port.

Solution was:

```auto
const hostname = window.location.hostname
const protocol = window.location.protocol
const url = `${protocol}//${hostname}:6000`
window.location.href = url

```

Since you use http:// it interprets it as a completely new site and not a path.  
I believe.

If you want the browser to open a new site use:

```auto
window.open(`${protocol}//${hostname}:6000`, `_blank')

```

Also when using a button to trigger the linking in the template node, remember to do an if check (like a string sent from the button) in the script before sending the user to another side. Before window.open(some site)  
If you dont, the javascript will execute the moment the user opens the site.

Hope this was clear.

Best regards  
Steffen

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 February 2023 15:42 UTC](https://discourse.nodered.org/t/button-template-node-link-to-another-site/74748/4 "2023-02-21T15:42:19Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
