11

how do I automatically execute javascript?

I know of <body onLoad="">, but I just thought maybe there is another way to do it?

html:

<html><head></head><body><div id="test"></div></body></html>

javascript:

<script>(function(){var text = document.getElementById('test').innerHTML;var newtext = text.replace('', '');return newtext;})();</script>

I wanna get the text within "test", replace certain parts, and then output it to the browser.

Any ideas on how to do it? I'd appreciate any help. Thanks.

YOU
  • 106,832
  • 29
  • 175
  • 207

5 Answers5

19

If you don't want to use <body onload> which is good choice in terms of obtrusive javascript, you can separate that and put you code like this:

window.onload = function(){
  // your code here
};

Alternative:

Place your javascript code at the bottom of the page.

Sarfraz
  • 355,543
  • 70
  • 511
  • 562
6

Place the script at the bottom of the page, outside the closing body tag..

Sky Sanders
  • 33,086
  • 5
  • 65
  • 88
  • Does this ensure the DOM has been created? Do you have any spec/source for this, by any chance? – Alsciende Apr 15 '10 at 11:39
  • @Alsciende - Umm... yeah, the dom is created as it is parsed. The body was just closed so the document is ready. For sure. No doubt. Take my word for it. ...your cue to prove my arrogance ;-) – Sky Sanders Apr 16 '10 at 14:31
  • Most likely obvious for experiences Javascripters, but a reminder for inexperienced like me: place after the body, not just `callFunction()`. – Suma Mar 21 '16 at 14:14
4

It's REALLY easy! If you have a script in your "head" block, with no function id, it will run automatically as soon as the web page loads. For example:

<head> <meta charset="UTF-8"> <title>Redirection to www.mywebsite.org</title>

<!-- This script initiates an automatic web page redirection, as the page is loaded -->
<script type="text/javascript">
window.location = "http://www.mywebsite.com/"
</script>

</head>

user2993258
  • 91
  • 1
  • 1
  • 1
    +1 I think this is the real answer to the question, at least as I understand the question. 'Cause you still need to execute `window.onload = function()` bit somehow, right? – ultracrepidarian Aug 11 '16 at 01:50
1

If you don't want to use jQuery, use native window.onload method:

<script type="text/javascript">
    function ReplaceText() {
        document.getElementById('test').innerHTML = document.getElementById('test').innerHTML.replace(/abc/g, "def");
    }
    window.onload = ReplaceText;
</script>

Used on the code:

<div id="test">abc abc</div>

Will give this output:

def def
rochal
  • 7,243
  • 2
  • 30
  • 53
0

A quick way, if you just want to debug, would be move what you want to execute outside of a function.

<script type="text/javascript">
var text = document.getElementById('test').innerHTML;
var newtext = text.replace('', '');
alert(newtext);
</script>

NB. I'm not sure what you hope to achieve with text.replace('', '') ?

Dan Diplo
  • 24,377
  • 4
  • 61
  • 86
  • i did that, but the alert wouldnt popup ;) just used text.replace('','') to shorten the code thanks –  Apr 15 '10 at 11:07