3gwebtrain
3gwebtrain

Reputation: 15299

javascript create and append element function not work

this is my javascript function :

function createLink (){
    var link = document.createElement("a");
    link.href = "#";
    var linkText = document.createTextNode("This is dynamic link");
    link.appendChild(linkText);
    document.body.appendChild(link);
}

window.onload = createLink;

but i am not get any result from this. any one know, what is wrong with my code?

Upvotes: 1

Views: 376

Answers (1)

Nick Craver
Nick Craver

Reputation: 630559

What you have works, you can test it here. Make sure that whenever you're running this isn't after window.onload has already fired, otherwise it won't run.

Also, make sure something else isn't stealing the onload event, and setting the handler to something other than createLink, for example a window.onload = otherFunction later down the line.

Upvotes: 5

Related Questions