For example, i want my HTML page can be able to call a javascript function automatically once it is loaded or refreshed. I don't want to trigger the function by using a form and submit button.
How can I do so? And what is the coding? Many thanks.
[Edited] Okay it is working right now. Many thanks to all you guys and now I have one more question. Actually I am using servlet to print out the HTML tag to make a HTML page. I am using servlet since i want to load some data from Mysql database and then pass it as parameter to the javascript to carry out some calucation. So my last question is How can I control the time of calling javascript? I want to call read database first and triggering the javascript function (at the same time passing the data read as parameter) .How can I do that? Really thanks for your answer!
For example, i want my HTML page can be able to call a javascript function automatically once it is loaded or refreshed. I don't want to trigger the function by using a form and submit button.
How can I do so? And what is the coding? Many thanks.
[Edited] Okay it is working right now. Many thanks to all you guys and now I have one more question. Actually I am using servlet to print out the HTML tag to make a HTML page. I am using servlet since i want to load some data from Mysql database and then pass it as parameter to the javascript to carry out some calucation. So my last question is How can I control the time of calling javascript? I want to call read database first and triggering the javascript function (at the same time passing the data read as parameter) .How can I do that? Really thanks for your answer!
Share Improve this question edited Mar 7, 2012 at 11:54 hippietrail 17k21 gold badges109 silver badges179 bronze badges asked Mar 7, 2012 at 8:43 08091 Yin08091 Yin 1191 gold badge3 silver badges13 bronze badges 1-
<script type="text/javascript">window.onload=someFunction;</script>
or<body onLoad="someFunction()">
– Tomasz Nurkiewicz Commented Mar 7, 2012 at 8:45
9 Answers
Reset to default 3JavaScript Call Function After Page Load
<body onload="myFunc() ;">
or
<script>
window.onload=myFunc ;
</script>
refer http://www.mkyong./javascript/javascript-call-funtion-after-page-load/
<body onload="myfunction();" />
The browser fires an onload event when the page is loaded. You can attach it like this -
<body onload="readyFunction()">
My page
<script>
function readyFunction(){
//your js function
}
</script>
</body>
<body onload="yourfunction()">
<body onLoad="FunctionName()">
you mean something like this ?
<BODY onLoad="function1()">
Using jquery is highly remended. see here http://www.learningjquery./2006/09/introducing-document-ready
$(document).ready(function() {
// put all your javascript here.
});
try this
<script>
window.onload = function() {
// ...
}
</script>
or
<script>
window.onload = functionName;
</script>
or you can just call it as
<body>
...
<script>
functionName();
</script>
</body>
or on body load
<body onload="functionName();">
check this link http://grizzlyweb./webmaster/javascripts/refresh.asp
To answer the second part of your question, if you want to pass data from your jsp to a javascript function, you will have to convert your data into a json string, using libraries like Jackson, or Gson, and put in the string as a variable in your javascript.
SOmething like this -
<% String jsonData = getJSONFromObject(dataObject); %> //getJSONFromObject could be a util function which internally uses jackson or gson for conversion
<script>
var jsonStr = "<%= jsonData %>";
var dataObj = JSON.parse(jsonStr);
</script>
YOu can use a JSON lib like https://github./douglascrockford/JSON-js/blob/master/json2.js for converting the JSON string to an object.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744128408a4559722.html
评论列表(0条)