티스토리 툴바

다음의 코드를 살펴보자.

<html>
<head>
<script type="text/javascript">
  function aaaa(msg){
     alert(msg);
  }
</script>
<head>
<body>
<script type="text/javascript"> 
        var a = '<p id="p1" onclick="aaaa("Welcome to my world!!");">This is test.</p>';        
        var b = '<p id="myP" onclick="alert(this.innerText)">Welcome to my world!!</p>';        
        document.write(a);
        document.write(b);
</script>
</body>
</html>

위의 코드를 실행하면 다음과 같이 나타난다.
This is test.
Welcome to my world!!

그렇다면, 각 문장을 클릭한다면?
첫째 문장을 클릭하면 어떻게 되는가?
그렇다면 둘째 문장을 클릭하면 어떻게 될까?

둘째 문장은 구현한 대로 alert()에 의해 Welcome to my world!! 팝업 메시지가 뜰 것이다.

하지만 첫번째 문장은 아무런 꿈쩍도 안한다. 

Why??

바로 특수문자인 ' 또는 " 문자열 중복으로 생긴 오류이다. 
Javascript 에서 특수 문자를 사용하기 위해서는 \(back slash) escape 기호로 특수문자인 것을 알려주면 해당 특수문자 코드는 특수문자체로 사용할 수 있다.

onclick="aaaa("Welcome to my world");" 
이와 같이 사용하면 이벤트 처리시 어떠한 형식으로도 정상적으로 실행되지 않는다.

다음과 같이 사용하자.
onclick="aaaa('Welcome to my world');"
또는
onclick='aaaa("Welcome to my world");'
또는
onclick=\"aaaa(\'Welcome to my world\');\"

여기서는 다음과 같이 수정해 보자.
var a = '<p id="p1" onclick=\"aaaa(\'Welcome to my world!!\');\">This is test.</p>';        

<p> 태그를 감싸고 있는 ' 기호와 내부의 " 그리고 ' 기호가 중복됨으로 \ 기호를 추가한 것이다.

Posted by Zzoe
예제
<script type="text/javascript">
  document.write('aaa</br>');
</script>
<html>
  <head>
    <script type="text/javascript">
      document.write('bbb</br>');  
    </script> 
  </head>
  <body>
  ABC</br>
  <script type="text/javascript">
    document.write('ccc</br>');
  </script>
  </body>
</html>
<script type="text/javascript">
  document.write('ddd</br>');
</script>

결과
aaa
bbb
ABC
ccc
ddd

결과와 같이 Javascript는 html 어느 위치에 들어가도 상관없다. 다만 실행순서의 차이만 있을 뿐이다.

여기서 주의할 점이 있다. 
만약, onload 이벤트에서 document.write()를 실행하면 어떻게 될까?

위의 예제에서 다음 코드를 추가해 보자.

<body onload="javascript:document.write('AAAAA');">
또는 
<script type="text/javascript">
  function window.onload(){
    document.write('AAAAA');
  }
</script>


<script type="text/javascript">
  document.write('aaa</br>');
</script>
<html>
  <head>
    <script type="text/javascript">
      document.write('bbb</br>');  
    </script> 
  </head>
  <body onload="javascript:document.write('AAAAA');">
  ABC</br>
  <script type="text/javascript">
    document.write('ccc</br>');
  </script>
  </body>
</html>
<script type="text/javascript">
  document.write('ddd</br>');
</script>

결과
AAAAA


이게 어떻게 된 것인가?

Javascript는 onload 이벤트를 기준으로 문서 파싱 전 / 후로 나눌 수 있다.
즉, 문서 파싱 전(onload 이벤트 발생 전) document.write()는 파서에 의해 결과를 문서 중간에 삽입하고 파싱된다.
하지만, 문서 파싱 후(onload 이벤트 발생 시) document.write()는 문서 전체를 덮어써 버린다.
따라서, onload 이벤트 처리시 document.write()를 쓸 경우에는 조심해야 한다.



Posted by Zzoe
2009 Crown Oaks Day

Keep the following in mind

1. Change to master change.    
2. Anyone who has never made a mistake has never tried anything new.
3. God doesn't ask about out ability, but out availability.
4. Fear of the future is a waste of the present.


1. 변화를 정복하려면 변화하라.
2. 한번도 실수한 적이 없는 사람은 한 번도 새로운 것에 도전해 본 적이 없는 사람이다.
3. 신은 우리의 능력을 묻지 않는다. 다만 오직 우리의 가능성을 물을 뿐이다.
4. 미래를 두려워하는 것은 현재를 낭비하는 것이다.
Posted by Zzoe