This is my code
var clicked = false;
function bodyUnload() {
alert(1);
if (clicked == false)//browser is closed
{
var request = GetRequest();
request.open("GET", "../TestLogout.aspx", true);
request.send();
}
}
function GetRequest() {
var xmlhttp;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
In Chrome it functions properly. When I click the browser's close button the TestLogout.aspx.cs page is called and in the page load function of TestLogout.aspx.cs page I have used Session.Abandon(); to abandon the session if the user clicks the close button. It is working fine with chrome but not with firefox and internet explorer. Can any one tell what is happening?
Related
I use the simple AJAX and use google debug then find that the url is not exist...
The code is very simple:
var http;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
http=new XMLHttpRequest();
} else {
http=new ActiveXObject("Microsoft.XMLHTTP");
}
try {
http.open("GET", 'http://'+ip+':5000/test.html', true);
http.onreadystatechange = onRcvData;
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
http.send(null);
} else {// code for IE6, IE5
http.send();
}
} catch(e) {
http.abort();
}
function onRcvData() {
if (http.readyState==4) {
if (http.status==404) {
} else if(http.status==200) {
} else {
}
}
}
It's okay if the file test.html exists.
When the file isn't exist, the error show in the part:
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
http.send(null);
} else { // code for IE6, IE5
http.send();
}
So, even if I use the onreadystatechange method cannot prevent the error...
The file is in a directoy beside my web pages.
Then what should I do to combine with httprequest?
Any advice appreciate.
Add:
I have used the method 'Head' but it is return 404... (No matter jQuery plugin/javascript)
Like the picture:
What should I do...
Is the direction of the error I found misleaded?
Try this function
function urlExists(testUrl) {
var http = jQuery.ajax({
type:"HEAD", //Not get
url: testUrl,
async: false
})
return http.status!=404;
}
//Usage
if(!urlExists('http://www.mysite.com/somefileOrImage.ext')) {
alert('File not found');
}
HEAD
The HEAD method is identical to GET except that the server MUST NOT
return a message-body in the response. The metainformation contained
in the HTTP headers in response to a HEAD request SHOULD be identical
to the information sent in response to a GET request. This method can
be used for obtaining metainformation about the entity implied by the
request without transferring the entity-body itself. This method is
often used for testing hypertext links for validity, accessibility,
and recent modification.
Read about head here
you can use this function
function UrlExists(url)
{
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
var http=new XMLHttpRequest();
}
else {
var http=new ActiveXObject("Microsoft.XMLHTTP");
}
http.open('HEAD', url, false);
http.send();
return http.status!=404;
}
Reference
Is it possible to trigger a php function every minute or so using Javascript without going to another page?
You can make a loop in your PHP script and use the sleep() function.
The doc for sleep() here
Or you can do that
var milliSeconds = 6000;
setInterval( function() {
var xmlhttp;
if (window.XMLHttpRequest) // code for IE7+, Firefox, Chrome, Opera, Safari
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
console.log ( xmlhttp.responseText );
}
}
xmlhttp.open("POST","functions.php",true);
xmlhttp.send();
}, milliSeconds);
You have to load xmlhttp request object according to the browser ( xmlhttp=new XMLHttpRequest(); ), then set an event handler when the xmlhttp state changes ( xmlhttp.onreadystatechange=function() ). When it changes check if the status is 200 (success) then do whatever you want with the response. ( I printed it to console )
I am trying to display two javascript function when page load. But seems one is overwriting another one.
here is javascript code:
function welcome(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("welcome").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","php/display_welcome.php", true);
xmlhttp.send();
}
function testimonial(){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("testimonial").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","php/display_testimonial.php", true);
xmlhttp.send();
}
window.onload = function() {
welcome();
testimonial();
};
I have 2 div id in the html page.
Also I am using IE9, when I run only one function it works fine, when run two it doesnt return the data.
Hope someone could help me.
A lot could have gone wrong:
Non-responsive server
Bad copy-pasted code
Syntax errors
etc.
Anyways, to avoid repepetive code and possible errors even if both code looks the same, I suggest you do this. Since both basically use the same operation, let's factor it out into a single reusable function:
function load(url,callback) {
var xmlhttp;
if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
else xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
callback(xmlhttp.responseText);
}
}
xmlhttp.open('GET', url);
xmlhttp.send();
}
window.onload = function () {
load('php/display_welcome.php',function(response){
document.getElementById("welcome").innerHTML = response;
});
load('php/display_testimonial.php',function(response){
document.getElementById("testimonial").innerHTML = response;
});
};
You declared xmlhttp without var which gives it global scope, that is why it gets over written when you call the second function. Declare it locally and that will not happen. Use var and declare it locally.
var xmlhttp;
Im trying to setup a redirect for a chat script. If the chat goes unanswered after x amount of time the page will redirect.
I posted a question here yesterday regarding the same thing, but at the time knowing little in regards to JS I was trying to mix php with js. I have changed tactics.
Here is what I got thus far:
function opCheck()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
opCheck();
// alert('working2');
}
}
opjoined = "newchattimer.php";
xmlhttp.open("GET",opjoined,true);
xmlhttp.send(null);
}
function opResult()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
//alert('state = 4');
var op = xmlhttp.responseText;
}
}
ajaxurl = "ajaxfiles/opAnswer_12.txt";
xmlhttp.open("GET",ajaxurl,true);
xmlhttp.send(null);
}
setTimeout(function() {
opCheck();
opResult();
//alert(op);
if (op == 'n') window.location.replace("chatnoop.php");
}, 3000);
It creates the text file properly but ultimately no redirect. I used the chrome deveolpers tool and no errors. I also tried to alert(op); to see if the result is being grabbed, but I get no alert.
What is wrong with this code?
Thanks.
If you had taken the time to indent your code, you'd see that var op is declared inside the scope of the onreadystatechange function, which is both out of scope and asynchronous.
function opResult() {
var xmlhttp;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} else {
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
var op = xmlhttp.responseText; //declared here
}
}
ajaxurl = "ajaxfiles/opAnswer_12.txt";
xmlhttp.open("GET", ajaxurl, true);
xmlhttp.send(null);
}
setTimeout(function () {
opCheck();
opResult();
// op is out of scope here
if (op == 'n') window.location.replace("chatnoop.php");
}, 3000);
Since timeouts aren't generally the way to handle async functions, and doing ajax request when all you intend to do when it finishes is to redirect anyway is'nt really neccessary, you could just do a regular form submit, which will redirect all by itself, or move the redirecting inside the right scope!
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if(xmlhttp.responseText == 'n') window.location.href = 'chatnoop.php';
}
}
You may find what you want here http://www.tizag.com/javascriptT/javascriptredirect.php but basically i don't think that window.location.replace("chatnoop.php"); is correct. You should consider using JQuery to manage all the Ajax stuff in addition, but it should work like you've done anyway
I have these two buttons that a user can click to either approve/deny somebody on a site, and the code works perfect in IE, but when I try and use firefox, nothing at all happens when I click the buttons.
the javascipt/ajax code is:
function ApproveOrDenyStudent(i, action){
if (window.XMLHttpRequest){
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var newStudentEmail = "newStudentEmail" + i;
var emailField = document.getElementById(newStudentEmail);
var email = emailField ? emailField.value : '';
// Approve/deny the user
if (action == 0){
xmlhttp.open("GET","ApproveStudent.php?email="+email,true);
}
else if (action == 1){
xmlhttp.open("GET","DenyStudent.php?email="+email,true);
}
xmlhttp.send();
window.location.reload();
}
any help would be great! Thanks!
You got a race condition!
xmlhttp.send();
window.location.reload();
You are making an asynchronous call. You are making the Ajax request and replacing the page right away! The call to the server is not getting out.
Reload the page when the request is complete.
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4){
window.location.reload(true);
}
};
xmlhttp.send();