\r\n\r\n\r\n해당 구분을 펑션? 으로 변경하고 싶습니다.\r\n( 초보라 설명이 잘 되었는지 모르겠네요...ㅠㅠ)\r\n\r\n \r\n\r\n  function loadScripts() {\r\n    function start() {\r\n      this.client = new window.Client({     \r\n        Daily: {\r\n          \"idname\",\r\n          \"passname\",\r\n           1\r\n        }\r\n       });\r\n      this.client.start();\r\n    }\r\n\r\n    const script = document.createE...","dateCreated":"2024-11-01T15:49:19+09:00","author":{"@type":"Person","name":"복이온디야"},"answerCount":7,"suggestedAnswer":[{"@type":"Answer","text":"[code]\r\n\r\nfunction loadScripts() {\r\n  // Daily 함수를 호출하는 함수 정의\r\n  function start() {\r\n    // Daily가 전역 함수라고 가정\r\n    if (typeof Daily === 'function') {\r\n      Daily('idname', 'passname', 1);\r\n    } else {\r\n      console.error('Daily function is not loaded');\r\n    }\r\n  }\r\n\r\n  // 스크립트 로드\r\n  const script = document.createElement('script');\r\n  script.src = \"https://example.com/ex.js\";\r\n  \r\n  // 스크립트 로드 완료 후 실행될 콜백\r\n  script.onload = () => {\r\n    start();\r\n  };...","author":{"@type":"Person","name":"마르스컴퍼니"},"dateCreated":"2024-11-01T20:11:41+09:00"},{"@type":"Answer","text":"32767고개....\r\n","author":{"@type":"Person","name":"미니님a"},"dateCreated":"2024-11-01T17:22:06+09:00"},{"@type":"Answer","text":"아닙니다.\r\nfunction Daily(idname, passname, index) {\r\n    if (!document.querySelector('script[src^=\"/ex.js\"]')) {\r\n        const script = document.createElement('script');\r\n        script.src = `/ex.js`;\r\n        script.id = 'idJS';\r\n        document.head.appendChild(script);\r\n    }\r\n}\r\n\r\nDaily('idname', 'passname', 1);\r\n\r\nex.js 파일에 값을 전달하는 게 아니고,\r\n해당 파일을 head 에 삽입하는 함수입니다.\r\n단순히 head 에 과 같은 역할을 하는 겁니다.\r\n때문에 굳이 위처럼...","author":{"@type":"Person","name":"메이드"},"dateCreated":"2024-11-01T17:06:38+09:00"},{"@type":"Answer","text":"ex.js 가 로드 된 후 Daily 함수를 호출 하고 싶은 것 같은데 복잡한 코드가 필요한가요?\r\n\r\n\r\n\r\n","author":{"@type":"Person","name":"균이"},"dateCreated":"2024-11-01T17:04:15+09:00"},{"@type":"Answer","text":"위 값이 어떤 역할을 하는지..모르겠지만,\r\nfunction Daily(idname, passname, index) {\r\n}\r\n와 같이 함수에 인자를 넣어주신 후\r\nDaily('idname', 'passname', 1); 이렇게 호출하시면 됩니다.\r\n","author":{"@type":"Person","name":"메이드"},"dateCreated":"2024-11-01T16:58:15+09:00"},{"@type":"Answer","text":"function addModalJS() {\r\n    if (!document.querySelector('script[src^=\"/ex.js\"]')) {\r\n        const script = document.createElement('script');\r\n        script.src = `/ex.js`;\r\n        script.id = 'idJS';\r\n        document.head.appendChild(script);\r\n    }\r\n}\r\n\r\n단순히 스크립트 파일을 동적으로 추가하는 거라면, 간단하게 하시는 게 좋을 듯 싶네요.\r\n","author":{"@type":"Person","name":"메이드"},"dateCreated":"2024-11-01T16:43:17+09:00"},{"@type":"Answer","text":"어떤 로직을 원하시는지 정확히 알수있을까요?\r\n","author":{"@type":"Person","name":"리오닥터"},"dateCreated":"2024-11-01T16:27:47+09:00"}]}}

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

<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개 / 댓글 7개

Copy
function loadScripts() {
  // Daily 함수를 호출하는 함수 정의
  function start() {
    // Daily가 전역 함수라고 가정
    if (typeof Daily === 'function') {
      Daily('idname', 'passname', 1);
    } else {
      console.error('Daily function is not loaded');
    }
  }

  // 스크립트 로드
  const script = document.createElement('script');
  script.src = "https://example.com/ex.js";
  
  // 스크립트 로드 완료 후 실행될 콜백
  script.onload = () => {
    start();
  };
  
  // 스크립트 로드 실패 시 에러 처리
  script.onerror = () => {
    console.error('Failed to load script');
  };

  // 스크립트 추가
  document.head.appendChild(script);
}

// 함수 실행
loadScripts();

답변에 대한 댓글 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개

단순히 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); 이걸 평션형식으로 전달하려고 하는데 잘 되질 않네요

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개

호출이 아니라 반대로

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);

이렇게 셋팅 해보았으나 작동이 안되는거 같습니다 ㅠ
호출이 아니라 반대로
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 내용이 궁금하네요 ㅎ

위 값이 어떤 역할을 하는지..모르겠지만,
function Daily(idname, passname, index) {
}
와 같이 함수에 인자를 넣어주신 후
Daily('idname', 'passname', 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에 해당값이 전달 되나요?

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개

감사합니다. 궁금증이 있어 남갑니다.

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

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

답변에 대한 댓글 1개

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

</script>


이걸

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

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