简体   繁体   English

加载网页后如何触发javascript功能(不使用表单和提交按钮)

[英]How can I trigger javascript function once I load the webpage (Not using form and submit button)

For example, i want my HTML page can be able to call a javascript function automatically once it is loaded or refreshed. 例如,我希望我的HTML页面能够在加载或刷新后自动调用javascript函数。 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. 实际上我使用servlet打印HTML标签来制作HTML页面。 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. 我正在使用servlet,因为我想从Mysql数据库加载一些数据,然后将它作为参数传递给javascript来进行一些calucation。 So my last question is How can I control the time of calling javascript? 所以我的最后一个问题是如何控制调用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? 我想首先调用read数据库并触发javascript函数(同时将数据作为参数传递)。我该怎么做? Really thanks for your answer! 真的谢谢你的回答!

JavaScript Call Function After Page Load 页面加载后的JavaScript调用函数

<body onload="myFunc() ;">

or 要么

<script>
window.onload=myFunc ;
</script>

refer http://www.mkyong.com/javascript/javascript-call-funtion-after-page-load/ 参考http://www.mkyong.com/javascript/javascript-call-funtion-after-page-load/

<body onload="myfunction();" />

The browser fires an onload event when the page is loaded. 加载页面时,浏览器会触发onload事件。 You can attach it like this - 你可以像这样附上它 -

<body onload="readyFunction()">
My page
<script>
function readyFunction(){
//your js function
}
</script>
</body>
<body onload="yourfunction()">

你的意思是这样的?

<BODY onLoad="function1()">
<body onLoad="FunctionName()">

Using jquery is highly recommended. 强烈建议使用jquery。 see here http://www.learningjquery.com/2006/09/introducing-document-ready 请参阅http://www.learningjquery.com/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.com/webmaster/javascripts/refresh.asp 查看此链接http://grizzlyweb.com/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. 要回答问题的第二部分,如果要将数据从jsp传递到javascript函数,则必须使用像Jackson或Gson这样的库将数据转换为json字符串,并将其作为字符串放入你的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.com/douglascrockford/JSON-js/blob/master/json2.js for converting the JSON string to an object. 你可以使用像https://github.com/douglascrockford/JSON-js/blob/master/json2.js这样的JSON库来将JSON字符串转换为对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM