How can I change the script to open itself after the page is fully loaded?

I want to change the script so that it will automatically load after the page is fully loaded, but it does not work for me.

I tried like this

<script type='text/javascript'>
//<![CDATA[
var la=!1;window.addEventListener("load",function(){(0!=document.documentElement.loadTop&&!1===la||0!=document.body.loadTop&&!1===la)&&(!function(){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-ххххххх";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(e,a)}(),la=!0)},!0);
//]]>
</script>

But this way it opens immediately, not after the page is fully loaded.

This is how the rules work, but it is shown after scrolling, how to make it appear itself after the page is fully loaded?

<script type='text/javascript'>
//<![CDATA[
var la=!1;window.addEventListener("scroll",function(){(0!=document.documentElement.scrollTop&&!1===la||0!=document.body.scrollTop&&!1===la)&&(!function(){var e=document.createElement("script");e.type="text/javascript",e.async=!0,e.src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-хххх";var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(e,a)}(),la=!0)},!0);
//]]>
</script>

Why the browser has a beforeunload event even though I do not use it in my codes?

I have a function to help the user scroll up to the top of the page like this:

function toTheTop(){
        document.getElementByID('topUpBtn').addEventListener('click', ()=>{
            scrollTo( { top: document.querySelector('h1').getBoundingClientRect().top,
        behavior: 'smooth'})
    });
}

export {
    toTheTop
}

I am using webpack for code organisation. I called toTheTop function in an entry point as below:

import { toTheTop } from './js/TopUpButton.js';

if(document.readyState !== 'loading') {
    console.log('document is already ready');
    toTheTop();
} 
else{
    document.addEventListener('DOMContentLoaded', ()=>{
        console.log('document was not ready'),
        toTheTop();
    });
}

export {
    toTheTop
}

I have tested this function without webpack and it worked. However, when using with webpack, it didn’t. Inspect the console for the button I only see:

DOMContentloaded
click

But with webpack setup, I see a weird line named “beforeunload” in order like this:

beforunload
click

open this line I saw:

self.addEventListener("beforeunload", function () {
  status.isUnloading = true;
});

I did not implement this code at all. I look for the use of beforeunload, this only uses if we want to prompt something for user before leaving a page.

My questions are: Why do I have the beforeunload function in my code? Does this relate to my webpack setup? And How can I solve my problem?

I tried to figure it out for several days, but still can’t handle it. Really need your help!

jQuery How To Use Backticks In Append

I wanted to interpolate variables in strings in JS so I used “(backticks) as shown here –
How To Interpolate Variables In String in JS

Then, I Wanted To put IF-Statements in jQuery Append So I got this –
IF Statements In jQuery Append

But When I use Both Together , Backticks Don’t Output Text As Usual-

$("main").append(`Hello ${my_var}`+(second_var>1?"hi ":"bye")+`Bye ${my_var}`

This Results Only In “hi” , The Backticks Before And After The Ternary Operator Don’t Output Anything.
HELP ??

Text flows over navbar

So, been working on this for about 2 weeks now on JUST the navbar itself. I have tried so many things that i don’t recall what i have used to try to fix it.

It works on my old navbar (but it’s boostrap 3 and this project is on 5). The text overflows my navbar and causes the text/website to get extra space then what it’s supposed to be showing. I need a genius to resolve this for me as i have been stumped on this for a long time.

Issue (images)
https://gyazo.com/c212a931d6770b77998cb3fea3ae87d1 i’m highlighting it as the text can’t be seen due to white backround.

Trying to get working (old navbar) https://gyazo.com/66c482022e325cd9bd335ff666377474

Here is a JSfiddle to give an example plus the code i’m using. https://jsfiddle.net/yf90uxhc/2/

HTML navbar code

    <div class="container-fluid">
      <a class="navbar-brand" href="/">Home</a>
      <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">

        <ul class="navbar-nav me-auto mb-2 mb-lg-0">
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Amarr Empire
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Caldari State
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Minmatar Republic
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Gallente Federation
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Guristas Pirates
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Angel Cartel
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Blood Raider Covenant
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              ORE
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Servant Sisters of Eve
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Mordu's Legion Command
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Sansha's Nation
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Serpentis
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              Triglavian Collective
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
              EDENCOM
            </a>
            <ul class="dropdown-menu">
              <li><a class="dropdown-item" href="#"> Frigate Class</a>
                 <ul class="submenu dropdown-menu">
                  <li><a class="dropdown-item" href="#">Frigate</a>
                    <ul class="submenu dropdown-menu">
                      <li><a class="dropdown-item" href="#"> </a></li>
                  </ul>
                  </li>
               </ul>
              </li>
              </ul>
          </li>
          <!-- -->
        </ul>
      </div>
    </div>
  </nav>```

Javascript
```document.addEventListener("DOMContentLoaded", function(){
    // make it as accordion for smaller screens
    if (window.innerWidth < 992) {
    
      // close all inner dropdowns when parent is closed
      document.querySelectorAll('.navbar .dropdown').forEach(function(everydropdown){
        everydropdown.addEventListener('hidden.bs.dropdown', function () {
          // after dropdown is hidden, then find all submenus
            this.querySelectorAll('.submenu').forEach(function(everysubmenu){
              // hide every submenu as well
              everysubmenu.style.display = 'none';
            });
        })
      });
    
      document.querySelectorAll('.dropdown-menu a').forEach(function(element){
        element.addEventListener('click', function (e) {
            let nextEl = this.nextElementSibling;
            if(nextEl && nextEl.classList.contains('submenu')) {    
              // prevent opening link if link needs to open dropdown
              e.preventDefault();
              if(nextEl.style.display == 'block'){
                nextEl.style.display = 'none';
              } else {
                nextEl.style.display = 'block';
              }
    
            }
        });
      })
    }
    // end if innerWidth
    }); 
    // DOMContentLoaded  end```

Decorator function flow in javascript

I am trying to understand the flow of execution in this code. As you can see in the snippet, there is a function slow and a cachingDecorator function which takes a function as its parameter. Here we are passing function slow in it. After passing the function slow, the slow equals

    slow =  function(x) {
        if (cache.has(x)) { // if there's such key in cache
          return cache.get(x); // read the result from it
        }

        let result = func(x); // otherwise call func
        cache.set(x, result); // and cache (remember) the result
        return result;
      };

Now what I don’t understand is that when we call slow(1) are we passing the 1 to this slow function

function slow(x) {
      // there can be a heavy CPU-intensive job here
      console.log(`Called with ${x}`);
      return x;
    }

or in this

    slow =  function(x) {
        if (cache.has(x)) { // if there's such key in cache
          return cache.get(x); // read the result from it
        }

        let result = func(x); // otherwise call func
        cache.set(x, result); // and cache (remember) the result
        return result;
      };

function slow(x) {
  // there can be a heavy CPU-intensive job here
  console.log(`Called with ${x}`);
  return x;
}

function cachingDecorator(func) {
  let cache = new Map();
  return function(x) {
    if (cache.has(x)) { // if there's such key in cache
      return cache.get(x); // read the result from it
    }

    let result = func(x); // otherwise call func
    cache.set(x, result); // and cache (remember) the result
    return result;
  };
}

slow = cachingDecorator(slow);
console.log(slow);
console.log(slow(1));

How to solve the problem that the message cannot be used when the element-plus is introduced locally?

Recently I am trying to write element-plus casually, so I did not configure the Node environment and build the vue scaffolding. It was referenced locally through the way of cdn file download. All aspects of the components can be used normally, but I am trying to write the message prompt box js The components don’t seem to work. I don’t know how to introduce the components of the prompt box. The official ones are all used in the scaffolding npm way. I don’t know how to use these js components like cdn or local? I urge everyone to answer! Grateful!
The following is a code snippet:

<script src="/static/js/vue.global.js"></script>
<script src="/static/js/index.full.js"></script>
<script src="/static/js/axios.min.js"></script>
<script src="/static/js/qs.min.js"></script>
.then(function (response) {
  console.log(response);
  if (response.data == "error") {
      ElMessage('this is a message.')
  }
})

But the browser console reports an error:

ElMessage is not defined

New component for every column in Supabase database React

I have a Supabase database called threads with 5 entries, I am trying to make a new React component for every column in the database but I am running into some issues.

The issue that it’s giving me is that it’s stopping the program to prevent an infinite loop, I’d assume this is because in my code I am updating my useState every time I render my Component which I am aware of, but I’m not sure how I would get around this problem.

Component.js:

import {useState} from "react";
import {supabase} from "../../utils/supabaseClient";

export default function Sidebar() {
    const [newThreads, setNewThreads] = useState([]);

    // this is where I am kinda stuck
    const threadList = [];
    (async () => {
        let {data: threads, error} = await supabase
            .from('threads')
            .select('thread_title');
        threadList.push(threads); // how do I get the current index of the loop (and limit it to only get 5 entries?)
        threadList.forEach(function (value) {
            console.log(value)
        })
    })();
    setNewThreads(threadList);

    return (
        <div className="sidebar">
            <div className="sidebar-widget">
                <span className="widget-title">New Threads</span>
            </div>
            <div className="sidebar-widget">
                <span className="widget-title">New Members</span>
                {(Object.entries(newThreads) || []).map(([key, value]) => {
                    return (
                        <div className="widget-cell">
                            <div className={"widget-cell-image"}/>
                            <div className="widget-cell-content">
                                <span className={"widget-cell-title"}>{value}</span>
                                <div className="widget-cell-values">
                                    <span className={"widget-cell-by-user"}>by harley_swift,</span>
                                    <span className={"widget-cell-time"}>22:02</span>
                                </div>
                            </div>
                        </div>
                    );
                })}

            </div>
        </div>
    )
}

Any help with an explanation would be greatly appreciated!

How can I add a class to each option?

how can i add a class to each option? this is the PHP code, see screenshot in the description, can you help me modify this code, thanks

            <?php 
                if($params->get('cat_select_type','0')==1){
                    $cat_sel = '<select  class="inputbox" id="se'.$module->id.'_cat_0" name="se_cats[]" onchange="se'.$module->id.'_getFields(this.value);"><option value="">'.JText::_('PLEASE_SELECT_CATEGORY').'</option>';
                    foreach($categories as $cat){ 
                        $c_name = str_ireplace("'", "&apos;", $cat->name);
                        for($lev=0;$lev<$cat->level;$lev++){
                            $c_name ="- ".$c_name;
                        }                       
                        $cat_sel .= '<option value="'.$cat->id.'">'.$c_name.'</option>';
                    }
                    $cat_sel .= '</select>';
                    echo $cat_sel;
                }else{
                    $cat_sel = '<select  class="inputbox" id="se'.$module->id.'_cat_0" name="se_cats[]" onchange="se'.$module->id.'_new_cat(0,this.value,new Array());se'.$module->id.'_getFields(this.value);"><option value="">'.JText::_('PLEASE_SELECT_CATEGORY').'</option>';
                    foreach($categories[0] as $cat){
                        $cat_sel .= '<option value="'.$cat->id.'">'.str_ireplace("'", "&apos;", $cat->name).'</option>';
                    }   
                    $cat_sel .= '</select>';
                    echo $cat_sel;
                }                   
                ?>

screenshot

Does typescript have syntactic sugar around returning a value or throwing an exception if that value is undefined?

I find myself wanting to do something like this a lot (pseudocode):

const something = somethingElse || throw an error

This would throw an error if somethingElse were falsy. Here’s another example, written in real typescript:

const accessToken = process.env.ACCESS_TOKEN ?? throw new Error("The environment variable ACCESS_TOKEN was undefined or null");

This doesn’t compile because the right-hand side needs to be an expression. Is there a way to write code functionally equivalent to the above while being comparable in terseness?

I can do the following, but I find it obnoxious to type and it uses up a lot of vertical space:

  const accessToken = process.env.PATREON_CREATORS_ACCESS_TOKEN;
  if (accessToken === undefined) {
    throw new Error("The environment variable PATREON_CREATORS_ACCESS_TOKEN was undefined");
  }

I could also create some utility function where you pass in something potentially falsy, and if it is falsy, throw an error. But I’m wondering if typescript/javascript provides some clever syntax to pull this off so I don’t have to create such a function.

Can you set “t” tab to always be the same size in a textarea?

I have a textarea, which I am trying to insert tabs of the same size into.

As you can see in the example, the only difference in each line of the testString is the position of the t.

However, in the resulting textarea, the size of the tabs are different, seemingly dependent on how many characters are in front of it. This causes text after the tab to not be aligned properly.

In my perfect world, all of the 5s in the textarea would be aligned vertically, since each line is technically the same (five characters + one tab)

I am aware of the CSS property tab-size, and though this indeed changes the size of tabs, the resulting tab size in the textarea is still based on how many characters are in front of it.

I don’t want to insert spaces rather than tabs, because I want a single backspace/delete to remove the entire tab, but if someone enters multiple spaces in succession, I would like backspace/delete to only clear single spaces as per usual.

Is there something that I am missing? Are there any workarounds to achieve this? My research has returned no helpful results, so I am now reaching out here.

Thanks for your help.

testString = '1t2345' + 'n' + '12t345' + 'n' + '123t45' + 'n' + '1234t5'
document.getElementById('text').value = testString
<textarea cols="10" rows="4" id="text"></textarea>

Store json data into separate variables depending on category React [duplicate]

So i have this random api that generates random json data. Is there a way i can store them as variables separately based on category?
For ex:

const data = [
  { color: "red", category: "shoes", quantity: 10 },
  { color: "blue", category: "shoes", quantity: 20 },
  { color: "black", category: "jacket", quantity: 10 },
  { color: "white", category: "jacket", quantity: 20 },
  { color: "green", category: "hat", quantity: 10 },
];

I would like to have var jacket to have the data { color: "black", category: "jacket", quantity: 10 }, { color: "white", category: "jacket", quantity: 20 } then var shoes to have all the data with the category shoes and var hat to have all the hats. Note: the data is random so the color, category and quantity fields always change. Any help would be greatly appreciated.