9

How i can make some thing like this?

<div id='myDiv' onload='fnName()'></div>

can't use

window.onload = function () {
    fnName();
};

or

$(document).ready(function () {fnName();});

the div element is dynamic. The div content is generated by xml xsl.

Any ideas?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Harold Sota
  • 7,490
  • 12
  • 58
  • 84
  • How can an XSL append elements dynamically? Isn't it supposed only to transform XML to HTML? If you mean your HTML is generated through an XLS, the jQuery's `$(document).ready(f);` will still work since it's triggered when the DOM is ready. – Albireo Feb 17 '11 at 10:30
  • see also: http://stackoverflow.com/questions/4057236/how-to-add-onload-event-to-a-div – Ciro Santilli OurBigBook.com Nov 25 '14 at 12:26

9 Answers9

10

You can use DOM Mutation Observers

It will notify you every time the dom changes, e.g. when a new div is inserted into the target div or page.

I'm copy/pasting the exmple code

// select the target node
var target = document.querySelector('#some-id');

// create an observer instance
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation.type);
  });    
});

// configuration of the observer:
var config = { attributes: true, childList: true, characterData: true }

// pass in the target node, as well as the observer options
observer.observe(target, config);

// later, you can stop observing
observer.disconnect();
alexandru.topliceanu
  • 2,364
  • 2
  • 27
  • 38
10

The onload attribute probably wouldn't fire on the <div> if you're injecting it dynamically (as the document is likely already loaded, but maybe it'd still work...?). However you could either poll for the element by simply doing something like this (similar to YUI's onContentAvailable):

// when the document has loaded, start polling
window.onload = function () {
    (function () {
        var a = document.getElementById('myDiv');
        if (a) {
            // do something with a, you found the div
        }
        else {
            setTimeout(arguments.callee, 50); // call myself again in 50 msecs
        }
    }());
};

Or you could change the markup (I know nothing about XSL) to be something like this:

Earlier on in the page:

<script type="text/javascript">
    function myDivInserted() {
        // you're probably safe to use document.getElementById('myDiv') now
    }
</script>

The markup you generate with XSL:

<div id="myDiv"></div>
<script type="text/javascript">
    myDivInserted();
</script>

It's a bit hacky but it should work.

Dan Beam
  • 3,632
  • 2
  • 23
  • 27
  • Oh, I may have mis-interpreted your question, but if fnNamt() or fnName() need the `
    ` you'll need to find a way to defer until that `
    ` is present.
    – Dan Beam Feb 17 '11 at 09:17
4

If you're not already using jQuery there's no reason to start using it just for this, you can write:

window.onload = function () {
    fnName();
};
Albireo
  • 10,977
  • 13
  • 62
  • 96
3

You could use jQuery. The following code would be place in your <head> tags.

$(document).ready(function() {
    // Your fnNamt function here
});

EDIT

Kobi makes a good point

You could also write $(document).ready(function(){fnNamt();});, or more simply, $(document).ready(fnNamt);, or even $(fnNamt)

Neil Knight
  • 47,437
  • 25
  • 129
  • 188
  • 2
    You could also write `$(document).ready(function(){fnNamt();});`, or more simply, `$(document).ready(fnNamt);`, or even the confusing `$(fnNamt);`. – Kobi Feb 17 '11 at 08:47
  • I can't use these the div element is dinamyc.The div content is generated by xml xsl. – Harold Sota Feb 17 '11 at 08:48
  • 2
    @Haroldis - you question should definitely mention that... Try editing it to include much more details. – Kobi Feb 17 '11 at 08:49
  • 1
    hi Ardman..maybe he doesn't want to include a library to do something like this.. it would be good to find out how jQuery implements this feature :) – stecb Feb 17 '11 at 08:50
2

I had the same Issue, and after searching I found this.

In my case, the javascript appends the head of the index html to load a tab content html file, and onload I want to add that tab to the dom, display it and make other js stuff to change the tabs style.

I added the line with .onload = function(event) {...}

var link = document.createElement('link');
link.rel = 'import';
link.href = 'doc.html'
link.onload = function(event) {...};
link.onerror = function(event) {...};
document.head.appendChild(link);

This worked like a charm, and maybe it helps some other researcher :)

I found it on HTML5 Imports: Embedding an HTML File Inside Another HTML File

murthag11
  • 21
  • 3
  • This only works for a link, image or iframe (anything with a href or src). Not for any other standard html element – mplungjan Nov 06 '19 at 08:28
2

Without jQuery with plain JS eg:

<script type="text/javascript">
function bodyOnLoad() {
  var div = document.getElementById('myDiv');
  // something with myDiv
  ...
}
</script>

<body onload="bodyOnLoad()">
....
<div id='myDiv'></div>
....
</body>
evilcroco
  • 521
  • 4
  • 8
1

How about using jQuery/ready(..) for this?

Like here: http://api.jquery.com/ready#fn

In the context of your question,

$(document).ready(function () {fnNamt();});
UltraInstinct
  • 43,308
  • 12
  • 81
  • 104
0

I would suggest you to use jQuery.
Steps:
1. Download Jquery library from here http://code.jquery.com/jquery-1.5.min.js .
2. in your HTML, head section create a script tag and use this code below.

$(document).ready(function() {
// write your code here..
});

rass
  • 17
  • 1
0

I'd suggest circle-style func:

SwitchFUnc = false;

function FuncName(div_id) {
    var doc = window!=null ? window.document : document;
    var DivExists = doc.getElementById(div_id);

    if (DivExists) {
        //something...
        SwitchFunc = true;  //stop the circle
    }
}

while (SwitchFunc!=true) {
    FuncName('DivId');
}
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Archinamon
  • 171
  • 1
  • 12
  • 1
    This would be a very bad thing to do. You would spike the CPU on a busy wait. – HaxElit Feb 09 '12 at 17:57
  • 1
    This is a terrible idea. JavaScript is single-threaded and blocks the UI thread. The fact that it is running, blocking until something happens, prevents that thing it is waiting for from happening, causing a deadlock. – icktoofay Aug 11 '12 at 22:57