1

Possible Duplicate:
How to add onload event to a div?

I am trying to draw a line with css and javascript as in the example here: example. I can get it to work just fine with the original stuff but after trying to modify it and then get it to work onload from the html onload equals its not working, but it is working with the onclick event.

here is my css:

.line 
{
    position: absolute;
    height: 0px;
    border-width: 2px 0px 0px 0px;
    border-style: solid;
    border-color: #99aadd;
 }

and the javascript:

function createLine2(myline,x1,y1,x2,y2)
{
    if (x2 < x1)
    {
        var temp = x1;
        x1 = x2;
        x2 = temp;
        temp = y1;
        y1 = y2;
        y2 = temp;
    }
    var length = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
    myline.style.width = length+"px";
    var angle = Math.atan((y2-y1)/(x2-x1));
    myline.style.top = y1 + 0.5*length*Math.sin(angle) + "px";
    myline.style.left = x1 - 0.5*length*(1 - Math.cos(angle)) + "px";
    myline.style.MozTransform = myline.style.WebkitTransform = myline.style.OTransform="rotate("+angle+"rad)";
}

and finally the html that does not work:

<div class="line" onload="createLine2(this,100,100,0,0)">test</div><br>

and the html that does work:

<div class="line" onclick="createLine2(this,100,100,0,0)">test</div><br>

How do I get it to work with onload and what did I mess up?

4
  • 9
    DIVs do not have a load event Commented Dec 27, 2012 at 23:13
  • A simple googling of "div onload" would have given you the answer. Commented Dec 27, 2012 at 23:15
  • @musa then how could i get it to create the line in that div onload Commented Dec 27, 2012 at 23:16
  • Why is this tagged jQuery? Commented Dec 28, 2012 at 0:08

2 Answers 2

2

The onload Event is not allowed on div Tags. It's only allowed with the following tags:

<body>, <frame>, <frameset>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>

http://www.w3schools.com/jsref/event_onload.asp

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using jquery, I suggest using the jquery ready function.

.ready( handler ) handlerA function to execute after the DOM is ready.

http://api.jquery.com/ready/

If you are using plain JavaScript, use window.onload

window.onload = funcRef;

https://developer.mozilla.org/en-US/docs/DOM/window.onload

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.