# Using Javascript to Track an open browser tab for updates

**URL:** <https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164>\
**Category:** General\
**Created:** [May 23, 2020, 5:53pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164 "2020-05-23T17:53:11Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![noam2](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@noam2](https://forum.freecodecamp.org/u/noam2)\
**Post date:** [May 23, 2020, 5:53pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/1 "2020-05-23T17:53:12Z")

</div>

Hello Everyone!

My javascript is very basic and I was looking online for a way to this but couldn’t find one.

My work has a unique webpage I log into in order to recieve support calls. It has an indicator that counts new callers at the top of the page that I inspected in chrome and saw it has a unique html **id="…"** tag.

Can I use javascript/chrome console to listen to this specific part of the page and make a sound when it dynamically updates (“number of calls” text changes)?

Thanks!  
Noam

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 6:07pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/2 "2020-05-23T18:07:45Z")

</div>

Yes you can.

Which part is updating? The html?

---

<div class="post-metadata">

**Author:** ![noam2](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@noam2](https://forum.freecodecamp.org/u/noam2)\
**Post date:** [May 23, 2020, 6:59pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/3 "2020-05-23T18:59:28Z")

</div>

The text itself is updating. I’m pretty sure it’s the html because when Inspect it I can see the actual number within a span tag and when it changes the chrome inspector resets.

![image](https://global.discourse-cdn.com/freecodecamp/original/3X/8/6/86f3f90bf011c750f5070e4d25f8d7b59abe7151.png)

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 7:18pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/4 "2020-05-23T19:18:05Z")

</div>

```auto
const getElement = (element) => document.querySelector(element);

const init = () => {
	const $span = getElement('#ELEMENT_ID_GOES_HERE');
    const options = { subtree: true, childList: true };
 	const observer = new MutationObserver((mutationsList, observer) => {
  	for (let mutation of mutationsList){
    	if (mutation.type === 'childList')
             alert('change made!');
    }
  });
  
  observer.observe($span, options);
};

init();

```

The other way would be to use setTimeout and recursively check the value.

---

<div class="post-metadata">

**Author:** ![noam2](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@noam2](https://forum.freecodecamp.org/u/noam2)\
**Post date:** [May 23, 2020, 8:02pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/5 "2020-05-23T20:02:52Z")

</div>

Thanks for that! It looks like exactly what I was looking for. However, console returns **“Uncaught TypeError: Failed to execute ‘observe’ on ‘MutationObserver’: parameter 1 is not of type ‘Node’.”**

I understand that it means that the element does not exist when the functions searches for it and, as you said, it requires setTimeout.

I tried and it doesn’t work, sorry if I made a basic mistake 😬:

> const getElement = (element) =\> document.querySelector(element);
> 
> const init = () =\> {  
> const $span = getElement(‘myid’);  
> if(!$span) {  
> window.setTimeout(init,500);  
> return;  
> }  
> const options = { subtree: true, childList: true };  
> const observer = new MutationObserver((mutationsList, observer) =\> {  
> for (let mutation of mutationsList){  
> if (mutation.type === ‘childList’)  
> alert(‘change made!’);  
> }  
> });
> 
> observer.observe($span, options);  
> };
> 
> init();

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:18pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/6 "2020-05-23T20:18:48Z")

</div>

No no. The setTimeout suggestion is if you didnt use MutationObserver.

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:20pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/7 "2020-05-23T20:20:00Z")

</div>

Are you forgetting the `#` in front of your id?

You don’t need this part:

```auto
if(!$span) {
window.setTimeout(init,500);
return;
}

```

So remove that.

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:23pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/8 "2020-05-23T20:23:34Z")

</div>

example.

```auto
<span id="blah">4</span>

```

should be  
` const $span = getElement('#blah');`

and not  
`const $span = getElement('blah');`

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [May 23, 2020, 8:27pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/9 "2020-05-23T20:27:55Z")

</div>

Hey, it’s not just `getElement` you have to be specific, there are a lot of ways to get an element.

```auto
.getElementsByClass();
.getElementsByTagName();
.getElementsByName();
.getElementById();
.querySelector();
.querySelectorAll();

```

In your guy’s case, it’s `.getElementById();`, because you guys are trying to grab an element that has an id so that would look like this:

```auto
document.getElementById("id-name"); //No need the # on 

```

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:28pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/10 "2020-05-23T20:28:47Z")

</div>

@Catalactics  
Go back and reread beginning of my code.

```auto
const getElement = (element) => document.querySelector(element);

```

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [May 23, 2020, 8:32pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/11 "2020-05-23T20:32:46Z")

</div>

@kerafyrm, Now it makes sense, sorry for the confusion. That’s not a normal practice for me.

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:34pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/12 "2020-05-23T20:34:47Z")

</div>

@Catalactics  
It helps make code more readable in larger projects.

typing  
`document.querySelector('.blah')`  
`document.getElementById('blah')`

vs  
`getElement('.blah')`  
`getElement('#blah')`

---

<div class="post-metadata">

**Author:** ![noam2](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@noam2](https://forum.freecodecamp.org/u/noam2)\
**Post date:** [May 23, 2020, 8:51pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/13 "2020-05-23T20:51:06Z")

</div>

I originally tried all the choices you guys suggested, with/without #, getElement, getElementById… all returned the node not found error.

In the new code with the setTimeout, it keeps returning “undefined”

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:53pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/14 "2020-05-23T20:53:33Z")

</div>

You dont use setTimeout() at all.

Do this, right click element, click inspect, then right click element in DOM:

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/5/9/59210550416eaec19166e5d417ead486ee66e2aa.png)

And plug that value in there.

---

<div class="post-metadata">

**Author:** ![kerafyrm](https://avatars.discourse-cdn.com/v4/letter/k/13edae/32.png) [@kerafyrm](https://forum.freecodecamp.org/u/kerafyrm)\
**Post date:** [May 23, 2020, 8:54pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/15 "2020-05-23T20:54:38Z")

</div>

If your element is inside an iframe then there’s several other annoying steps you’ll have to do to get the element.

---

<div class="post-metadata">

**Author:** ![noam2](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@noam2](https://forum.freecodecamp.org/u/noam2)\
**Post date:** [May 23, 2020, 8:57pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/16 "2020-05-23T20:57:23Z")

</div>

The “copy selector” copies the same id I already used.

Now that I checked the source code I see it uses an Iframe like you suggested

---

<div class="post-metadata">

**Author:** ![noam2](https://avatars.discourse-cdn.com/v4/letter/n/c0e974/32.png) [@noam2](https://forum.freecodecamp.org/u/noam2)\
**Post date:** [May 23, 2020, 9:00pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/17 "2020-05-23T21:00:48Z")

</div>

The Iframe refers to a .aspx file that when I go into shows the entire upper portion of the original call screen and seems like it keeps auto refreshing.

---

<div class="post-metadata">

**Author:** ![Catalactics](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/catalactics/32/151364_2.png) [@Catalactics](https://forum.freecodecamp.org/u/Catalactics)\
**Post date:** [May 23, 2020, 10:19pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/18 "2020-05-23T22:19:08Z")

</div>

Yeah it is, but it’s harder for other people to catch it without putting in comments when you’re declaring the variable.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:52pm UTC](https://forum.freecodecamp.org/t/using-javascript-to-track-an-open-browser-tab-for-updates/393164/19 "2021-06-01T18:52:13Z")

</div>

Closed on June 1, 2021, 6:52pm UTC


