Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace text with on page load

Tags:

javascript

I know how to replace text with getElementById() function in such situation:

<script type="text/javascript">
    function go(){
        var p = document.getElementById('ccc');
        p.firstChild.nodeValue = 'AAA';
    }
</script>

<div id='ccc'>o</div>

<a href="javascript:go()">go</a>

When I click on go link 'o' replaces on 'AAA'.

But I want to replace text when page loading to browser without any clicking. So user do not have to know that there was some text 'o' in div 'ccc'. He must see only 'AAA' from the beginning.

How to do that?

like image 613
Vitalii Ponomar Avatar asked Mar 21 '26 14:03

Vitalii Ponomar


1 Answers

You can use the onload event:

window.onload = go;

// or better
window.addEventListener("load", go);

You can also use an anonymous function:

window.onload = function()
{
  // ...
}
window.addEventListener("load", function()
{
  // ...
});
like image 188
ComFreek Avatar answered Mar 23 '26 05:03

ComFreek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!