Log in

View Full Version : Javascript not working properly in Chrome and Safari



zomb1e
08-23-2012, 12:33 AM
Hey Guys

I'm trying to display my text through lightbox using getElementbyid().innerHTML for which i'm passing variable from php

It is working absolutely fine in Firefox, Opera, and IE. But when I open it in Chrome and Safari, my text gets displayed for a second and then disappear or do not get displayed at all.

Below is my Javascript



function openLightBox(imgsrc, xx){

document.getElementById('light').style.display='block';
document.getElementById('fade').style.display='block';

document.getElementById('light-image').src = imgsrc;
showUser(xx);

}

function closeLightBox(){
//document.getElementById('light-image').innerHTML = '';
document.getElementById('light').style.display='none';
document.getElementById('fade').style.display='none';
}
function showUser(str)
{
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("div1").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","form2.php?q="+str,true);
xmlhttp.send();
}



Below is the code through which i'm passing php variable to JS function,



<a onClick="openLightBox('images/<?php echo $imgsrc ?>', '<?php echo $text ?>');" class="enlarge"><img src='images/<?php echo $imgsrc ?>' width="150" height="150" /></a>;


I know there is no problem with my script as it is working fine in rest of the browsers (except Chrome and Safari).

Can anyone please help. Thanks in advance!!!!!

jscheuer1
08-23-2012, 02:11 AM
Those browsers will not do a local XMLHttpRequest.

Put the page up live. If there's still a problem with it, give us a link to it.

Both Chrome and Safari have good error reporting if you know how to use it. I'm most familiar with Chrome. Right click anywhere on the page. Choose "Inspect Element". Once the inspector is launched, click on the console icon. If there are any errors they will be reported there.


As a side note, the PHP code should be immaterial. As long as it works in some browsers, it will be the same in all. PHP is server side. However, if the code it produces isn't cross browser, then there can be a problem. It's not with PHP, but with the HTML, javascript, and/or other client side code it produces. These can be checked in the same way that other client side code is checked - with validators for HTML and CSS, and with the browser's script console for javascript.