[js] ]innerHTML로 <script> 넣기

Posted by RAY.D
2015. 4. 16. 05:38 Web/javascript / jQuery
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.





obj.innertHTML = "<font size=2>가나다<\/font>";

와 같이 잘 사용을 하지만

 

obj.innertHTML = "<script>var a='가나다';<\/script>";

와 같은 script tag 는 무시된다.

 

 

이런경우,

 

1.

obj.innerHTML="<scr"+"ipt>var a='가나다';<\/scr"+"ipt>";

와 같이 script 문자를 떼어내어 사용한다.

 

혹은

 

2.

document.write("<script>var a='가나다';<\/script>");

를 시도한다.

 

혹은

 

3.

insertAdjacentHTML을 사용하여 SCRIPT tag에 DEFER 옵션을 시도한다.

 

Remarks

If the text contains HTML tags, the method parses and formats the text as it is inserted.

You cannot insert text while the document is loading. Wait for the onload event to fire before attempting to call this method.

When using the insertAdjacentHTML method to insert script, you must include the DEFER attribute in the scriptelement.

Example

This example uses the insertAdjacentHTML method to insert script into the page.

var sHTML="<input type=button onclick=" + "go2()" + " value='Click Me'><BR>";
var sScript='<SCRIPT DEFER>';
sScript = sScript + 'function go2(){ alert("Hello from inserted script.") }';
sScript = sScript + '</script' + '>';
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);