OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Capturing load event on LINK

  • Thread starter Thread starter pgn
  • Start date Start date
P

pgn

Guest
I'm trying to attach an event handler to the load event of a link tag, to execute some code after a stylesheet has loaded.

Code:
new_element = document.createElement('link');
new_element.type = 'text/css';
new_element.rel = 'stylesheet';
new_element.href = 'http://domain.tld/file.css';
new_element.addEventListener('load', function() { alert('foo'); }, false);
document.getElementsByTagName('head')[0].appendChild(new_element)

I have tried onreadystatechange as well:

Code:
new_element.onreadystatechange = function() { alert('foo'); }

Unfortunately neither approach results in an alert being triggered. Furthermore, new_element.onload is null after registering a handler for the load event with addEventListener. Is that normal?

PS: I may not use any framework in solving this.

<p>I'm trying to attach an event handler to the load event of a link tag, to execute some code after a stylesheet has loaded.</p>
<pre><code>new_element = document.createElement('link');
new_element.type = 'text/css';
new_element.rel = 'stylesheet';
new_element.href = 'http://domain.tld/file.css';
new_element.addEventListener('load', function() { alert('foo'); }, false);
document.getElementsByTagName('head')[0].appendChild(new_element)
</code></pre>
<p>I have tried <code>onreadystatechange</code> as well:</p>
<pre><code>new_element.onreadystatechange = function() { alert('foo'); }
</code></pre>
<p>Unfortunately neither approach results in an alert being triggered.
Furthermore, <code>new_element.onload</code> is null after registering a handler for the <code>load</code> event with <code>addEventListener</code>. Is that normal?</p>
<p>PS: I may not use any framework in solving this.</p>
 
Top