如何加载和使用来自 JavaScript 脚本的函数?

huangapple go评论52阅读模式
英文:

How to load and use functions from a js script?

问题

我正在加载一个外部的 JavaScript 文件在一个 React Next.js 页面上。
如何访问 init.js 中的函数 (FooInit)?

export default function Home() {
  useEffect(() => {
    FooInit() // 如何访问 FooInit?
  }, [])

  return (
    <>
      <Script beforeInteractive src="https://foo.boo/js/init.js" />
    </>
  )
}
英文:

I am loading an external js file on aReact Next js page.
How to access the function (FooInit) of init.js?

export default function Home() {
  useEffect(() =&gt; {
    FooInit() // how to access FooInit?
  }, [])

  return (
    &lt;&gt;
      &lt;Script beforeInteractive src=&quot;https://foo.boo/js/init.js&quot;
      /&gt;
    &lt;/&gt;
  )
}

答案1

得分: 1

要在React Next.js组件中的外部JavaScript文件中访问FooInit函数,您可以按照以下步骤进行:

  • 确保在调用该函数之前已加载外部脚本。您可以使用Script组件的onLoad事件处理程序。
  • window对象中访问FooInit函数,因为它是一个全局函数。

以下是一个示例实现:

import React, { useEffect } from 'react';
import Script from 'next/script';

export default function Home() {
  useEffect(() => {
    if (typeof window !== 'undefined' && window.FooInit) {
      window.FooInit();
    }
  }, []);

  const handleScriptLoad = () => {
    if (typeof window !== 'undefined' && window.FooInit) {
      window.FooInit();
    }
  };

  return (
    <>
      <Script
        beforeInteractive
        src="https://foo.boo/js/init.js"
        onLoad={handleScriptLoad}
      />
    </>
  );
}

在这个示例中,handleScriptLoad函数将在外部脚本加载后调用。在此函数中,我们检查window对象中是否存在FooInit函数,如果存在则调用它。我们还在useEffect钩子中执行相同的检查和调用,以处理脚本已经被浏览器加载和缓存的情况。

通过按照这些步骤操作,您可以成功地在Next.js React组件中访问并调用外部JavaScript文件中的FooInit函数。

英文:

To access the FooInit function from the external JavaScript file in your React Next.js component, you can follow these steps:

  • Make sure the external script has loaded before calling the function.
    You can use the onLoad event handler of the Script component.
  • Access the FooInit function from the window object since it's a global
    function.

Here's an example implementation:

import React, { useEffect } from &#39;react&#39;;
import Script from &#39;next/script&#39;;

export default function Home() {
  useEffect(() =&gt; {
    if (typeof window !== &#39;undefined&#39; &amp;&amp; window.FooInit) {
      window.FooInit();
    }
  }, []);

  const handleScriptLoad = () =&gt; {
    if (typeof window !== &#39;undefined&#39; &amp;&amp; window.FooInit) {
      window.FooInit();
    }
  };

  return (
    &lt;&gt;
      &lt;Script
        beforeInteractive
        src=&quot;https://foo.boo/js/init.js&quot;
        onLoad={handleScriptLoad}
      /&gt;
    &lt;/&gt;
  );
}

In this example, the handleScriptLoad function will be called once the external script has loaded. Inside this function, we check if the FooInit function is available in the window object and call it if it exists. We also do the same check and call in the useEffect hook for cases when the script is already loaded and cached by the browser.

By following these steps, you can successfully access and call the FooInit function from the external JavaScript file within your Next.js React component.

huangapple
  • 本文由 发表于 2023年4月4日 15:08:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75926430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定