**首先,我很抱歉我的英语不好!
我正在尝试为某个网站编写用户脚本。 该网站每次点击或几秒钟都会更新自己。 (它使用ajax更新 View 而不刷新) 当用户处于特定 View 时,我的用户脚本必须附加一些 html 元素。 如何在每次 View 更改时运行我的代码? (在ajax之后)
该页面有一个名为 ajaxCall
的函数发送 ajax 请求并根据响应更改页面。
我尝试过这样的事情:
var source = window.ajaxCall;
window.ajaxCall = function(param){
source(param);
myFunc();
}
这不起作用,因为 ajaxCall
发送异步ajax请求,所以:首先,调用源函数(ajax请求开始),紧接着我的函数调用。 (在ajax请求成功并且 View 改变之前)
有没有办法在页面更新后立即运行我的代码?
非常感谢。
最佳答案
尝试使用JQuery Library
Example 1:
$.ajax({
url: "http://fiddle.jshell.net/favicon.png",
beforeSend: function( xhr ) {
xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
}
})
.done(function( data ) {
if ( console && console.log ) {
console.log( "Sample of data:", data.slice( 0, 100 ) );
}
});
example 2:
var jqxhr = $.ajax( "example.php" )
.done(function() {
alert( "success" );
})
.fail(function() {
alert( "error" );
})
.always(function() {
alert( "complete" );
});
// Perform other work here ...
// Set another completion function for the request above
jqxhr.always(function() {
alert( "second complete" );
});
关于javascript - 如何为动态网站编写用户脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29763856/