Post Reply 
[PROG] what's up with appendChild
Author Message
Tetris999
..............................

Posts: 2,388.2801
Threads: 298
Joined: 15th Apr 2007
Reputation: -6.7936
E-Pigs: 82.5377
Offline
Post: #1
[PROG] what's up with appendChild
I was just doing a little messing around with javascript and I got onto the topic of inserting nodes into the DOM. Basically, I used appendChild on the body element, appending a p element. Thing is, when you view the source of the page, the source page doesn't register that a p element was added to the page, yet you can clearly see a paragraph element on the display page. Why is that? It's confusing since you can't tell if your element was successfully added or not.

I'm guessing that it has something to do with the page being loaded first and the script being second.

Here's the code/page

Code:
1
2
3
4
5
6
7
8
9
10
11
12
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
</body>
<script type="text/javascript">
var x = document.createElement("p");
x.textContent = "blahhhhh";
document.body.appendChild(x);
</script>
</html>


MY SIG IS FUCKING DEAD
02/08/2012 08:35 AM
Find all posts by this user Quote this message in a reply
S7*
Sweet Dreams

Posts: 16,682.8360
Threads: 1,056
Joined: 3rd Apr 2007
Reputation: 14.29926
E-Pigs: 383.2269
Offline
Post: #2
RE: [PROG] what's up with appendChild
Yes you're right. Viewing the source file will show you the page your browser downloaded.

Use a proper DOM inspector which will show you the page as you see it - most browsers have them integrated now, just hit F12.
02/08/2012 09:40 AM
Find all posts by this user Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)

 Quick Theme: