13

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.

1

5 Answers 5

20

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.

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

2 Comments

What do you mean "Place your javascript code at the bottom of the page"? How exactly?
@MartinAJ I think they're referencing by using <script> ... </script> tags :)
7

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

3 Comments

Does this ensure the DOM has been created? Do you have any spec/source for this, by any chance?
@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 ;-)
Most likely obvious for experiences Javascripters, but a reminder for inexperienced like me: place <script>callFunction()</script> after the body, not just callFunction().
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>

1 Comment

+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?
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

Comments

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('', '') ?

1 Comment

i did that, but the alert wouldnt popup ;) just used text.replace('','') to shorten the code thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.