tags:

views:

183

answers:

6

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.

+2  A: 

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

Sky Sanders
Does this ensure the DOM has been created? Do you have any spec/source for this, by any chance?
Alsciende
@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
+2  A: 

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
thanks, works like a charm!
@user317005: You are welcome :)
Sarfraz
A: 

found a similar query. see if it is useful "answer" in SO itself.

ZX12R
A: 

Do yourself a big and huge favor and start using jQuery. It's so insanely much easier than writing oldschool JavaScript.

And, to do things automatically when the DOM is ready you can use something as simple as this:

$(function() {
    // This will be executed when the document is loaded and ready
});
Arve Systad
jQuery is the best thing since sliced bread, but it's not always feasible to switch to a JS framework during an existing project.
Neil Aitken
i am ;) i just wanted to figure out how to do it without jquery, but thanks!
I am not going to downvote ya but I am sick to death of people replying to simple javascript questions, that in no way mention nor indicate a requirement for a lib, pushing jquery as the be all and end all. aaaaaaaaaaarrrrrrggggghhhh just answer the question. ;-)
Sky Sanders
Adding a 20kb javascript file for this kind of tiny requirement is complete overkill
roryf
A: 

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
i did that, but the alert wouldnt popup ;)just used text.replace('','') to shorten the codethanks
A: 

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