0

Tried for several times to get below Javscript to execute on page load. Currently it works only on click of "Start" button.

How could this be made excited on load of the page?

<script type="text/javascript">
function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}
function start() {

    var active = document.getElementById("buttonstart").value == "stop";
    getFlashMovie("test").setProperty("src",  !active ? document.getElementById('url2').value: null);
    document.getElementById("start").value = active ? "buttonstart" : "buttonstop";
}
</script>

the html

  <input id="url2" type="text" value="rtmp://localhost/?streammovie=test"/>

The getFlashMovie("test").setProperty is a function to pass variable to the SWF file. On page load I want it to get executed rather than on button click.

6 Answers 6

2

To have your function execute on page load have such code:

window.onload = function() {
    start();
};
Sign up to request clarification or add additional context in comments.

Comments

1

what you could do is:

<body onload="start();">

if you consider using jQuery at all then you could wrap your code within:

$(document).ready(function(){

      //the code

});

the code would execute when the page has been fully loaded, but i do not advise adding the jquery library just so you can use this feature.

1 Comment

Okay like that, But.. then you would still need to click the button, the start function in itself does not launch the set property only when the button is clicked.
1

Try this:

<script type="text/javascript">
function getFlashMovie(movieName) {
    var isIE = navigator.appName.indexOf("Microsoft") != -1;
    return (isIE) ? window[movieName] : document[movieName];
}
function start() {

    var active = document.getElementById("buttonstart").value == "stop";
    getFlashMovie("test").setProperty("src",  !active ? document.getElementById('url2').value: null);
    document.getElementById("start").value = active ? "buttonstart" : "buttonstop";
}
start(); //added this
</script>

4 Comments

If the script is located in the <head> section it won't work as the elements do not exist yet.
Just put it in the footer, before the body tag.
Why force such thing when you can use the window.onload?
Thx that throws an exception like: I had this exception before when I was trying to make it work: getFlashMovie("test").setProperty is not a function [Break On This Error] (10 out of range 3)
1

Just make sure the DOM has been loaded before you attempt to access any elements. Try to run your start() function at the end of the HTML like this;

  </body>
  <script type="text/javascript">
    start();
  </script>
</html>

1 Comment

Okay like that, But.. then you would still need to click the button, the start function in itself does not launch the set property only when the button is clicked.
0

Use:

(function($){

 $(function(){
 // my code comes here
 });     

})(jQuery)

Comments

0

Use window.onload event to fire your code on load.


window.onload = function(){

//your code to execute
}


Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.