Working in FF, Chrome but not IE? Whats wrong?
My partner has created a script but he is offline at the moment so I am at开发者_开发技巧tempting to fix this myself.
Here is the code we currently have:
function progressStatus(Progress) {
// Get our progress status
$.get('http://www.site.com/progress/'+Progress, { }, function (response) {
// Eval our response
eval(response);
});
}
and then in our page, we have:
// Start our status checking
var Progress = $('#Progress').val();
ProgressStatus = setInterval('progressStatus("'+Progress+'")', 1000);
It works fine in Firefox, Chrome, all I can assume is it's looping and getting the new status in Firefox, Chrome but in IE, it only pings the progress once and then does nothing.
What is wrong with this? I'm not sure if any more code is needed, if it is I'll edit my question with more code.
Thank you.
It's a bit difficult to tell without the value of the response
, and without more context. What does the console say? Are there errors?
Also, why don't you get data from the server, and then parse the data and respond accordingly, which, among other advantages (like keeping client code in one place and enforcing structure), makes debugging cases like this much simpler? Much better than eval
ing it and just trusting that whatever comes back knows how to process itself.
Also as good practise, keep your javascript in javascript, and not strings which will be evaluated. So instead of your setInterval
, have an anonymous function:
ProgressStatus = setInterval(function(){
progressStatus( $('#Progress').val() );
}, 1000);
Does not work means about nothing. IN the future please give more details on what not working means. I am going to assume that it means it is never updating the new status on the get request and keeps grabbing the old one.
Need to stop caching
$.ajax({
url: "http://www.site.com/progress/"+Progress,
cache: false,
success: function (response) {
// Evil Eval our response
eval(response);
}
});
As there's no standard for setInterval
, the result of feeding a string as first parameter may differ between browsers. As MDC says:
code
in the alternate syntax, is a string of code you want to be executed repeatedly.
This is the behaviour Firefox and Chrome exhibit.
On the other hand, IE probably parses the string once and executes that one repeatedly. This optimizes things a bit, but your function will be called with exactly the same parameter each time.
Moreover, to quote MDC again
(Using this syntax is not recommended for the same reasons as using eval())
A solution would be to read the status of Progress
within the function that's called repeatedly:
function progressStatus() {
var Progress = $('#Progress').val();
// Get our progress status
$.get('http://www.site.com/progress/'+Progress, { }, function (response) {
// ↑ don't!
// Eval our response
eval(response); // don't!
});
}
This way you can simply call
setInterval(progressStatus, 1000);
But again, using a user modifiable input field within a URL (Progress
) and eval
to parse (?) a response without any kind of validation is very insecure. You should probably use a variable within a closure to maintian Progress
(apart from showing it to the user).
I guess the problem is cache issue in IE. Mostly IE will cache by default in the settings.
You should add one random number at the end, hope fully it should work.
function progressStatus(Progress) {
// Get our progress status
$.get('http://www.site.com/progress/'+Progress + "/rnd/" + Math.random(), { }, function (response) {
// Eval our response
eval(response);
});
}
You need to handle if any URL rewriting issue.
Is there a specific reason for the "" around Progress in your setInterval? Javascript does not care about types..
Try
ProgressStatus = setInterval(function(){ progressStatus(Progress); }, 1000);
IMO it's prettier, and more readable, but dunno if it'll solve your problem :)
I think you need to edit the second code this way:
$(document).ready(function(){
var Progress = $('#Progress').val();
ProgressStatus = setInterval('progressStatus("'+Progress+'")', 1000);
}
It may be in this way. Try this if useful. I am not pretty much aware about jQuery, but it would be a solution if I'm not wrong.
精彩评论