Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question HELP - Create HTML using window.onload + function

    Greetings

    I have been attempting to add HTML elements in my document during the window.onload to call an html injecting function using several different approaches, none of which are working. In Chrome's developer tools>scripts my most commonly occurring error is cannot call method (i.e. appendChild, innerHTML) on "null".

    Below are my 3 approaches, can anyone see why they are not working?

  • #2
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    APPROACH 1:

    window.onload = initAll();
    function initAll() {

    var imgStack = [
    "image1_ad",
    "image2_ad",
    "image3_ad"
    ];

    //Creates IMG elements in the HTML from the array names above

    for(i=0; i<imgStack.length; i++) {
    var sContainer = document.getElementById('slideShowContainer');
    var imgSrc = "cImg/" + imgStack[i] + ".png";
    var imgElement = "<img src='" + imgSrc + "' />";
    sContainer.appendChild(imgElement);
    }
    }

  • #3
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    APPROACH 2:

    window.onload = initAll();

    function initAll() {
    //Add Images using format: "imageName_xx", (NO comma on LAST image)
    var imgStack = [
    "grimreaper1_ad",
    "grimreaper2_ad",
    "grimreaper3_ad"
    ];

    //Creates IMG elements from the array names in the HTML
    for(i=0; i<imgStack.length; i++) {
    var sContainer = document.getElementById('slideShowContainer');
    var imgSrc = "cImg/" + imgStack[i] + ".png";
    var imgElement = "<img src='" + imgSrc + "' />";
    sContainer.innerHTML = imgElement;
    }
    }

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    APPROACH 3:

    window.onload = initAll();
    function initAll() {

    var imgStack = [
    "image1_ad",
    "image2_ad",
    "image3_ad"
    ];

    //Creates IMG elements in the HTML from the array names above

    for(i=0; i<imgStack.length; i++) {
    var imgSrc = "cImg/" + imgStack[i] + ".png";
    var imgElement = "<img src='" + imgSrc + "' />";
    document.getElementById('slideShowContainer').appendChild(imgElement);
    }
    }

  • #5
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    I find it best to use something like this:

    Code:
    window.onload=function(){
    	initAll();
    };
    function initAll() {
    	//Add Images using format: "imageName_xx", (NO comma on LAST image)
    	var imgStack = [
    		"grimreaper1_ad",
    		"grimreaper2_ad",
    		"grimreaper3_ad"
    	];
    
    	//Creates IMG elements from the array names in the HTML
    	for(i=0; i<imgStack.length; i++) {
    		var sContainer = document.getElementById('slideShowContainer');
    		var imgSrc = "cImg/" + imgStack[i] + ".png";
    		var imgElement = "<img src='" + imgSrc + "' />";
    		sContainer.innerHTML = imgElement;
    	}
    }
    If you don't use that "function" part the onload won't work.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •