테스트 사이트 - 개발 중인 베타 버전입니다

자바스크립트 펑션 변경? 질문 드립니다

복이온디야 1년 전 조회 2,195

<script src="https://example.com/ex.js"> </script>
<script type="text/javascript">
Daily('idname', 'passname', 1);

</script>

해당 구분을 펑션? 으로 변경하고 싶습니다.
( 초보라 설명이 잘 되었는지 모르겠네요...ㅠㅠ)

 

  function loadScripts() {
    function start() {
      this.client = new window.Client({     
        Daily: {
          "idname",
          "passname",
           1
        }
       });
      this.client.start();
    }

    const script = document.createElement('script');
    script.src = "https://example.com/ex.js";
    script.onload = () => {
      start();
    };
    document.head.appendChild(script);
  }

  loadScripts();

우선 이와같이 변경해보았는데 작동이 안되더라구요.

어디가 잘못되었는지 알려주시면 감사하겠습니다!

댓글을 작성하려면 로그인이 필요합니다.

답변 7개

1년 전

</p>

<p>function loadScripts() {

  // Daily 함수를 호출하는 함수 정의

  function start() {

    // Daily가 전역 함수라고 가정

    if (typeof Daily === 'function') {

      Daily('idname', 'passname', 1);

    } else {

      console.error('Daily function is not loaded');

    }

  }</p>

<p>  // 스크립트 로드

  const script = document.createElement('script');

  script.src = "<a href="https://example.com/ex.js";" target="_blank" rel="noopener noreferrer">https://example.com/ex.js";</a>

  

  // 스크립트 로드 완료 후 실행될 콜백

  script.onload = () => {

    start();

  };

  

  // 스크립트 로드 실패 시 에러 처리

  script.onerror = () => {

    console.error('Failed to load script');

  };</p>

<p>  // 스크립트 추가

  document.head.appendChild(script);

}</p>

<p>// 함수 실행

loadScripts();</p>

<p>

로그인 후 평가할 수 있습니다

댓글을 작성하려면 로그인이 필요합니다.

1년 전

32767고개....

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

복이온디야
1년 전
네..?

댓글을 작성하려면 로그인이 필요합니다.

1년 전

아닙니다.
function Daily(idname, passname, index) {
    if (!document.querySelector('script[src^="/ex.js"]')) {
        const script = document.createElement('script');
        script.src = `/ex.js`;
        script.id = 'idJS';
        document.head.appendChild(script);
    }
}

Daily('idname', 'passname', 1);

ex.js 파일에 값을 전달하는 게 아니고,
해당 파일을 head 에 삽입하는 함수입니다.
단순히 head 에 <script src="/ex.js"></script> 과 같은 역할을 하는 겁니다.
때문에 굳이 위처럼 할 이유도 없지요.
정확히 하고자 하시는 게 뭔지를 모르겠네요.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

복이온디야
1년 전
단순히 head 에 <script src="/ex.js"></script> 과 같은 역할을 하는건 질문 본문에서 적어놓은

const script = document.createElement('script');
script.src = "https://example.com/ex.js";
script.onload = () => {
start();
};
document.head.appendChild(script);
}

코드로 해결이 됩니다. 하지만

ex.js 에 Daily('idname', 'passname', 1); 이걸 평션형식으로 전달하려고 하는데 잘 되질 않네요

댓글을 작성하려면 로그인이 필요합니다.

1년 전

ex.js 가 로드 된 후 Daily 함수를 호출 하고 싶은 것 같은데 복잡한 코드가 필요한가요?

<script src="https://example.com/ex.js"> </script>
<script type="text/javascript">

function start(){

if( typeof Daily =='function') Daily('idname', 'passname', 1); //함수가 존재하면 실행

else setTimeout(start, 700);

}

start()

</script>

로그인 후 평가할 수 있습니다

답변에 대한 댓글 2개

복이온디야
1년 전
호출이 아니라 반대로

ex.js 가 로드 된 후 Daily('idname', 'passname', 1)를 ex.js로 전달하고 싶습니다.

해당구문을 참고조하여

function start() {
Daily('idname', 'passname', 1);
}

const script = document.createElement('script');
script.src = "https://example.com/ex.js";
script.onload = () => {
start();
};
document.head.appendChild(script);

이렇게 셋팅 해보았으나 작동이 안되는거 같습니다 ㅠ
균이
1년 전
호출이 아니라 반대로
ex.js 가 로드 된 후 Daily('idname', 'passname', 1)를 ex.js로 전달하고 싶습니다.
<=== 무엇을 하려는 것인지 모르겠군요

함수를 실행을 원한다면 다음처럼 해도 되겠는데...보아하니 함수는 아닌 것 같고
<script type="text/javascript">
window.onload = function(){ Daily('idname', 'passname', 1); }
</script>

Daily가 함수가 되었다가 ==> Daily('idname', 'passname', 1);
전달 값이 되었다가 ==> Daily: { "idname", "passname", 1 }
종잡을 수가 없네요

나오는 순서대로 실행한다는 것을 생각하면 다음처럼 값을 먼저 넣어주면....
<script>
Daily = { idname: "idname", pass : "passname", num : 1 }
</script>
<script src="https://example.com/ex.js"> </script>

사용하려는 ex.js 내용이 궁금하네요 ㅎ

댓글을 작성하려면 로그인이 필요합니다.

1년 전

위 값이 어떤 역할을 하는지..모르겠지만,
function Daily(idname, passname, index) {
}
와 같이 함수에 인자를 넣어주신 후
Daily('idname', 'passname', 1); 이렇게 호출하시면 됩니다.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

복이온디야
1년 전
감사합니다.

function addModalJS() {
if (!document.querySelector('script[src^="/ex.js"]')) {
const script = document.createElement('script');
script.src = `/ex.js`;
script.id = 'idJS';
document.head.appendChild(script);
}
}
function Daily(idname, passname, index) {
}
Daily('idname', 'passname', 1);

이렇게 호출하면

ex.js에 해당값이 전달 되나요?

댓글을 작성하려면 로그인이 필요합니다.

1년 전

function addModalJS() {
    if (!document.querySelector('script[src^="/ex.js"]')) {
        const script = document.createElement('script');
        script.src = `/ex.js`;
        script.id = 'idJS';
        document.head.appendChild(script);
    }
}

단순히 스크립트 파일을 동적으로 추가하는 거라면, 간단하게 하시는 게 좋을 듯 싶네요.

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

복이온디야
1년 전
감사합니다. 궁금증이 있어 남갑니다.

Daily('idname', 'passname', 1); 이 값은 어떻게 넣어줘야 하나요?

댓글을 작성하려면 로그인이 필요합니다.

어떤 로직을 원하시는지 정확히 알수있을까요?

로그인 후 평가할 수 있습니다

답변에 대한 댓글 1개

복이온디야
1년 전
<script src="https://example.com/ex.js"> </script>
<script type="text/javascript">
Daily('idname', 'passname', 1);

</script>


이걸

펑션형식으로 변경원합니다

댓글을 작성하려면 로그인이 필요합니다.

답변을 작성하려면 로그인이 필요합니다.

로그인