2

My code is as follows:

window.onload = initialise;

function initialise() {

    var objPForSubmitMsg = document.createElement('p');
    objPForSubmitMsg.setAttribute('class', 'submitmsg');

    var arObjForms = document.getElementsByTagName('form');

    for (i = 0; i < arObjForms.length; i++) {
        var objFormParent = arObjForms[i].parentNode;
        alert(objFormParent);
        objFormParent.insertBefore(objPForSubmitMsg, arObjForms[i]);
    }

} // end initialise().

I checked the function with alerts and it goes through. When I "view-source" for the page after the function initialise() is done, there are no new elements added.

So my first question would be as per subject: can new elements inserted with javascript be seen with view-source?

If yes, then what is wrong with my code above? Why it doesn't insert new element?

I also tried to call initialise() from a button, but nothing happens then either.

I'm new to javascript so any comments would be appreciated.

EDIT: Thanks everyone. Ok, view-source cannot see it...

Than if I pass my page to php and load it with: $page = file_get_contents("mypage.html"); , if I echo that back with: echo $page; then I guess the newly created elements will not appear there either?

If that is the case, how would you pass the whole thing including the newly js created elements to php?

Vlad
  • 820
  • 10
  • 29
  • you haven't given the `html` code, give that also. – Mritunjay Aug 09 '14 at 06:23
  • Nope, view source shows the HTML received from the sever. – Felix Kling Aug 09 '14 at 06:25
  • possible duplicate of [Best Way to View Generated Source of Webpage?](http://stackoverflow.com/questions/1750865/best-way-to-view-generated-source-of-webpage) –  Aug 09 '14 at 07:10
  • It sounds like you aren't really understanding that javascript runs in the browser ONLY. The browser gets your page source from your server and then in the browser (far from your server), it runs the javascript in the page. There is code that runs on the server (PHP in your case) and there is code that runs in the browser (javascript). The two are completely separate worlds. You don't intermix them. – jfriend00 Aug 09 '14 at 07:30
  • You either generate page elements on your server (in PHP in your case) and then echo that to the client when they request the page. Or, you generate new elements in javascript in the browser and your PHP never sees those elements which is generally not an issue in any way. – jfriend00 Aug 09 '14 at 07:31
  • Ok, yeah that's what I wanted to know. Yeas I generate elements for error messages both in javascript (for the benefit of the user on client side validation) and also in php on server side validation (by reading the html from calling page and cutting it so I'm able to insert new elements from php). I didn't know if the elements created in javascript would be seen when I load the page html in php. It's good to know they wont as that would create duplicates... – Vlad Aug 09 '14 at 13:25

4 Answers4

3

View Source in the browser shows you the original HTML source of the page - exactly what came from the server before any client side modifications have been made.

As such, it will NOT include any dynamic changes to the page made by javascript.

To see changes that have been made dynamically, use a DOM inspector. There is one built into Safari and Chrome and IE and Firebug is an add-on for Firefox. All will show you the entire DOM hierarchy, live exactly like it currently exists in the browser. In fact, you can even modify the live DOM yourself in the inspector.


Your current code is inserting an empty <p> tag which may not be visible because it's empty. If you put some content into the <p> tag, it successfully inserts one <p> tag into your page. It will only insert one because you only create one and then you try to insert the same tag before each form. You can see what your current code does here: http://jsfiddle.net/jfriend00/3fvbj7re/.

If you want a <p> tag inserted before each form in the page, you'd need to create a separate <p> tag for each insertion like this:

function initialise() {
    var arObjForms = document.getElementsByTagName('form');
    var objPForSubmitMsg;

    for (i = 0; i < arObjForms.length; i++) {
        objPForSubmitMsg = document.createElement('p');
        objPForSubmitMsg.innerHTML = "hello";    // give tag some content
        objPForSubmitMsg.setAttribute('class', 'submitmsg');
        var objFormParent = arObjForms[i].parentNode;
        objFormParent.insertBefore(objPForSubmitMsg, arObjForms[i]);
    }

}

window.onload = initialise;
jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • ahaaa, thanks jfriend00. So, how does it treat it if I try to insert the same element at several different spots in html? It just doesn't do anything? or it just inserts it at the first spot specified? or...? – Vlad Aug 09 '14 at 06:54
  • @Vlad - it moves the element from wherever it was to the last place you tried to insert it. – jfriend00 Aug 09 '14 at 07:03
2

The Dom elements you add at runtime, were not present when the first time your page was loaded. In other words, it wasn't a part of your original page.

When you view source of your original page, it just shows the HTMl, without executing any JS or CSS, since you only explore HTMl in the source.

Hence, even when you add dynamic html elements in a page, you won't be able to see them when you click view source.

To see those elements, you should use the Developer Console of a browser.

Manish Kr. Shukla
  • 4,447
  • 1
  • 20
  • 35
  • Thanks Manish. That makes sense. Than if I pass my page to php and load it with: $page = file_get_contents("mypage.html"); , if I echo that back with: echo $page; then I guess the newly created elements will not appear there either? If that is the case, how would you pass the whole thing including the newly js created elements to php? – Vlad Aug 09 '14 at 06:37
  • I guess, The output that you expect, can only be achieved if you do the rendering of elements on the server side. – Manish Kr. Shukla Aug 09 '14 at 06:48
0

If you want to see the current DOM you should use the code inspector (Developer Tools) or javascript console, not the source, which is what the original response body was.

In Chrome for example go to view->developer->developer tools

pixelearth
  • 13,674
  • 10
  • 62
  • 110
0

I would like to add that just because you can't see it with view-source, doesn't mean you can't access your newly created elements using document.getElementById('el-id') or something similar. Kinda off topic but it's important to note.

Jason
  • 813
  • 11
  • 13