반응형
String str_script = "";

str_script += "var html_string = '';";

str_script += "html_string += '<div>';";
str_script += "html_string += 'WebView 에 동적 HTML 에 추가';";
str_script += "html_string += '</div>';";

str_script += "document.body.insertAdjacentHTML('afterend', html_string);";

webView.loadUrl("javascript:" + str_script);

이 스크립트의 핵심은 document.body.insertAdjacentHTML('afterend', html_string) 이 부분이다.

 

Element.insertAdjacentHTML(position, html_text)

 

기준이 되는 Element 에 html_text 를 추가하는 것이다.

 

position 은 다음과 같이 4가지 위치를 갖는다.

 

beforebegin - Element 앞에

afterbegin - Element첫번째 child

beforeend - Element 의 마지막 child

afterend - Element 뒤에

 

position 에 따라 html_text 로 추가된 html 의 위치가 결정된다.

 

이미 있는 페이지에 레이어 정도 띄워주는 거라면 'afterend' 면 충분할 것 같다.

 

이걸 응용하면 실제 운영중인 페이지에 영향을 주지 않고도 앱에서만 뭔가를 추가로 사용하려 할 때 좋은 것 같다.

반응형

+ Recent posts