[英]How to call a function when a page loads in Javascript with Jquery
<script type="text/javascript" >
function getDetails() {
var IDex = getQueryStringVariableByName("GameID");
$.ajax({
type: "POST",
url: "http:/...",
data: "{'ItemID': '" + escape(IDex) + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var data = response.d;
$('#output').empty();
$.each(data, function (index, item) {
var str = "Title: " + item.Name + "<br /> Current Price: " + item.CurrentPrice + "<br /> Developer: " + item.Developer + "<br /> Genres: " + item.Genre
$('#output').append('<li>' + str + '</li>');
});
},
failure: function (msg) {
$('#output').text(msg);
}
});
}
function getQueryStringVariableByName(name) {
//use this function by passing it the name of the variable in the query
//string your are looking for. For example, if I had the query string
//"...?id=1" then I could pass the name "id" to this procedure to retrieve
//the value of the id variable from the querystring, in this case "1".
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
window.onload = GetDetails;
//$(document).ready( function () {
// getDetails();
//
</script>
I have tried multiple methods to get getDetails to run when the page loads. 我已经尝试了多种方法来在页面加载时运行getDetails。 I have tried the window.onload method, putting it in the body tag, and a few others but I can't seem to find a way to get it to load automatically.
我已经尝试了window.onload方法,将它放在body标签中,还有其他几个,但我似乎找不到让它自动加载的方法。
it should be getDetails or you could use: 它应该是getDetails或你可以使用:
$(document).ready(function(){
functionname();
});
To check if the compiler was actually reading your code when the page loads is 检查编译器是否在页面加载时实际读取代码
$(document).ready(function() {
getDetails();
});
function getDetails() {
alert("this is a try");
//or
console.log("this is a try");
}
and of course include the jquery framework to your document like 当然也包括你的文档的jquery框架
<script src = "path" type = "text/javascript"></script>
The best way to do is write it before you have to close the body tag 最好的方法是在必须关闭body标签之前编写它
Hope it helps! 希望能帮助到你!
I'm not sure I understand the question, but the way to run a function a after page loads in jQuery
is this: 我不确定我是否理解这个问题,但是在
jQuery
加载后页面运行函数的方法是这样的:
$(document).ready(function() {
// Your code goes here..
myFunction();
});
It doesn't work? 它不起作用?
$(function(){
getDetails();
});
$(document).ready(function() {
getDetails();
});
will work for sure. 肯定会工作的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.