The IDs and classes of HTML elements

I have two different implementations of a livechat script. On my sandbox site, the livechat is fixed to the bottom right of the page and scrolls with the window. However, on my live site (where this specific code comes from), it is attached to the footer.

On my sandbox site, I am only using the script and the div ID tags for the top and bottom, not the middle.

Here is the code snippet used on my live site:

<script type="text/javascript">
  (function() {
    var c = document.createElement('script');
    c.type = 'text/javascript'; c.async = true;
    c.src = "http://northamericahvac.smartertrack.com/ChatLink.ashx?config=0&id=stlivechat0";
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(c,s);
  })();
</script>
<style>
  div.fixed {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 300px;
    border: 3px solid #73AD21;
  }
</style>
<div class="fixed">
  <div id="stlivechat0"></div>
</div>​

The additional code is an attempt to format the livechat button so that it remains fixed to the bottom right of the page and scrolls along with it.

I have tried creating a div class to style elements within it, but it seems like the styling does not apply to the div ID tag. Could it be that the div ID already has its own formatting which overrides the div class?

I even added a colored bar with the code #73AD21 to test if it stays fixed, but even that doesn't work as expected.

Answer №1

It appears that your issue is not directly related to CSS. The problem with the script arises from wrapping any JavaScript code within

<script>
  ( javascript code here )()
</script>

This essentially instructs the code to be treated as a function and executed immediately upon processing. As a result, it runs before the <div> element (located below) is added to the page, causing it to fail in locating the DOM element with id="stlivechat0" (which is within the link).

You may have been advised to place the <script> tag either just before the closing </body> tag or after the <div> containing id="stlivechat0". While this recommendation aims to prevent such issues, an alternative approach could involve encapsulating the code in a named function and calling it later by its name.

The conventional times for executing such functions include the window.onload event (when all resources are loaded) or the document.ready event (once the DOM structure is completed). However, in this scenario, simply moving the <script> tag after the <div> should resolve the issue:

<style>
  div.fixed {
    position: fixed;
    bottom: 0;
    right: 0;
    width: 300px;
    border: 3px solid #73AD21;
  }
</style>
<div class="fixed">
  <div id="stlivechat0"></div>
</div>
<script type="text/javascript">
  ({
    var c = document.createElement('script');
    c.type = 'text/javascript'; c.async = true;
    c.src = "http://northamericahvac.smartertrack.com/ChatLink.ashx?config=0&id=stlivechat0";
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(c,s);
  })();
</script>

Please note that the script cannot run on StackOverflow due to being blocked since it is not served over https://.

However, you can observe that the CSS styling is applied successfully.


In addition to Sebastian's comment below, another method equivalent to placing your script on the window.onload event involves adding the defer attribute to it: <script src="..." defer>. This attribute is supported by over 95% of current browsers.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Craving assistance in coding permutations

Thank you for responding. I want to express my gratitude for your efforts in trying to assist me. Unfortunately, I have posted this problem on multiple websites and no one has been willing to help. Regarding my code, my objective is to count permutations. ...

Handle all link clicks on the webpage

My challenge is that some users are required to utilize a virtual desktop in order to access specific information on my website. However, within the virtual environment, there are links leading to external content that do not function properly. I am seekin ...

What steps can be taken to update the ID's of <tr> and <td> elements within a table after deleting a row?

I am working on a dynamic table with 5 rows, each row containing 3 columns - partNO, quantity, and a delete button. Each row is assigned a unique ID for identification purposes. If a user deletes the second row, I want the IDs of the remaining rows to be ...

Verifying that objects are eligible for garbage collection

My program in node.js receives a high volume of messages. Each time a message is received, I create a new object and pass the message content to it. Inside the constructor of the new object, various operations are performed, including some mongo tasks with ...

Changing HTML elements dynamically within an ng-repeat using AngularJS directives

I have devised an angular directive where I execute an ng-repeat. The fundamental purpose of this directive is to interchange itself with a distinct directive that depends on a value forwarded into the original directive: <content-type-directive type=" ...

Implement CSS to stack images upon zooming

On my menu page, I have two images that I want to display side by side in the web browser and stacked on top of each other in a column when viewed on mobile. Currently, with framework7's row and column classes, the images are positioned next to each o ...

What's causing this sluggish performance?

I'm in the process of developing a Google Chrome extension and I can't help but wonder why window.onload = loadPage; function loadPage() { document.getElementById('nav-robux-amount').innerHTML = '0'; console.log(" ...

`vue.js: li element tag numbers not displaying correctly`

Issue with Number list not displaying correctly using Vue.js I have tried sending it like this. Is there a fix for this problem? This is the code snippet I used to output in Vue.js: p(v-html="selectedProduct.description") Snapsho ...

Should I consolidate my ajax requests into groups, or should I send each request individually?

Currently working on an ajax project and feeling a bit confused. I have 3 functions that send data to the server, each with its own specific job. Wondering if it's more efficient to group all the ajax requests from these functions into one big reques ...

Unable to retrieve Angular Service variable from Controller

I am facing an issue with my Angular Service. I have set up two controllers and one service. The first controller fetches data through an AJAX call and stores it in the service. Then, the second controller tries to access this data from the service. While ...

Troubleshooting problems with Flask's render_template()

I've been working on a Flask web application that requires users to be logged in to access the contents and functionalities. However, I've encountered an issue where, sometimes after logging in, instead of loading the full home page, a blank "abo ...

Why is it unnecessary to include a callback when using a setState function from useState as an argument in useEffect?

While working with a function from my component in the useEffect arguments, it is recommended to write a callBack so that it is memorized and used as a dependency of the useEffect. Without this, there is a warning. However, when using the setState of use ...

Tips for making a slide-in animation that doesn't require adjusting the browser size

As I work on an animation where an img object and text slide in from outside the frame of the webpage upon page load, everything seems to be running smoothly. But there's one glaring issue: right at the start of the page load, the webpage resizes for ...

Is it possible to verify whether a function contains a call to another function within it?

Consider a scenario in which we have the following nested functions: function function1(n) { function function2() { function function3() { function function4() { return n * 2; } return function4() } return ...

Enabling draggable behavior for div elements on Mozilla Firefox

I've attempted the code mentioned in this forum but unfortunately, it's not working for me. I am currently using Firefox 15 and it seems to work fine in Chrome. Below is my code : <!DOCTYPE html> <head> <title>A Simple Dragg ...

flex child moves outside parent when available space shifts

My dilemma involves a flex container (A) containing two or more flex items stacked in a column. The top flex item (B) is a Bootstrap Collapse panel, while the bottom item (C) consists of two horizontal divs (D and E). The problem arises when the collapse p ...

Correcting the reference to "/" (root) for assets after relocating the site to a subdirectory

I currently have a website located in the public_html directory, where all assets (images, css, js, etc) are referenced using /asset_folder/asset. The "/" at the beginning ensures that the browser starts from the root and navigates through the directories. ...

The hyperlink is malfunctioning

Seeking assistance with this issue. The menu items below (Get on Amazon etc) are able to change the information displayed on the page, however, the first menu should redirect to my index page but it's not working for some reason. <!DOCTYPE html> ...

Creating a Dynamic List in JQuery Fancy Box for an iframe

I am new to using JQuery and I have a table with a checkbox. When the checkbox is clicked, I want a dynamic list to appear in a pop-up window. I've been having trouble implementing this. <html> <head> <label align="center">Select a ...

Challenge with adjusting opacity of background when hovering over a box

I've encountered an issue with the background transparency in the following demo. For some reason, it's not working properly. .figure .caption-mask:hover { background: rgba(0, 0, 0, 0.0); } I'm attempting to remove the opacity f ...