우리는 우리의 참조자가 우리의 로고를 표시하기를 원하는 곳에 자바 스크립트 파일을 배치하는 몇 가지 참조 프로그램 코드를 작성하고 있습니다. 뭔가 ...
<script type="text/javascript" src="https://www.oursite.com/refer.js.asp?var=1"></script>
asp 스크립트에서 단순히 다음과 같은 약간의 코드를 작성합니다.
document.write('<a href="oursite.com?referrer=somereferrer"><img src="logo.png"></a>');
이것은 테스트 페이지에서 실행할 때 작동하지만이 오류가 발생하는 상황을 발견했습니다.
Failed to execute 'write' on 'Document': It isn't possible to write into a document from an asynchronously-loaded external script unless it is explicitly opened.
유틸리티 페이지를 통해 스크립트를 동적으로 생성하고 페이지의 div에 추가하려고하면 오류가 발생합니다. 페이지가 이미로드되었으므로 스크립트 파일에서 document.write를 사용할 수 없습니다. 그래서 제가 궁금한 것은 스크립트 파일이 DOM 내의 위치를 결정하여 링크와 이미지를 다음과 같은 것을 사용하여 올바른 위치에 배치 할 수있는 방법이 있다는 것입니다.
var a = document.createElement('a');
var img = document.createElement('img');
img.src = img_url;
a.appendChild(img);
a.href = "http://www.oursite.com";
document.body.appendChild(a);
이 경우에는 본문에 직접 요소를 추가합니다. 내가 할 수 있어야 할 것은 스크립트 파일을 포함하는 요소에 추가하는 것입니다. 가능합니까? 그렇다면 어떻게? 감사.
요소에 추가 id
하고 스크립트 내에서 script
사용할 id
수 있습니다.
<script id="a1" type="text/javascript">
window.addEventListener('DOMContentLoaded', function() {
var img = document.createElement('img');
img.src = 'https://dummyimage.com/600x400/000/fff';
document.getElementById('a1').nextSibling.parentNode.insertBefore(img, document.getElementById('a1').nextSibling);
});
</script>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다