javascript wont load, no clear error - javascript

I have the following code, which will not work. The javascript gives no errors and appears to load fine. but clicking on a link will do nothing. An example of a link is:
Link 1<li>Link 2</li>
and the code:
var xmlHttp
var layername
var url
function update(layer, url) {
var xmlHttp=GetXmlHttpObject(); //you have this defined elsewhere
if(xmlHttp==null) {
alert("Your browser is not supported?");
}
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
document.getElementById(layer).innerHTML=xmlHttp.responseText;
} else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
document.getElementById(layer).innerHTML="loading";
}
//etc
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
function updateByPk(layer, pk) {
url = "get_auction.php?cmd=GetAuctionData&pk="+pk+"&sid="+Math.random();
update(layer, url);
}
function updateByQuery(layer, query) {
url = "get_records.php?cmd=GetRecordSet&query="+query+"&sid="+Math.random();
update(layer, url);
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
xmlHttp=new XMLHttpRequest();
}catch (e)
{
try
{
xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
return xmlHttp;
}
function makewindows(){
child1 = window.open ("about:blank");
child1.document.write(<?php echo htmlspecialchars(json_encode($row2["ARTICLE_DESC"]), ENT_QUOTES); ?>);
child1.document.close();
}

It may probably be due to the double-quote characters surrounding 'Ed Hardy'. Does this work:
Link 1<li>Link 2</li>

From the wonderful JSLint
You are missing semicolons after these
var xmlHttp
var layername
var url
xmlHttp.onreadystatechange = function() {
if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
document.getElementById(layer).innerHTML=xmlHttp.responseText;
} else if (xmlHttp.readyState==1 || xmlHttp.readyState=="loading") {
document.getElementById(layer).innerHTML="loading";
}
//etc
}
(e) is used 2x, change the second to 'ex'.
try
{
xmlHttp=new XMLHttpRequest();
}catch (e)
{
try
{
xmlHttp =new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {}
}
Try using single quotes for EVERYTHING in JS, and use double quotes for EVERYTING in PHP.
Get Firebug to see if there are any other syntax errors.

Additionally, this line:
child1.document.write(<?php echo htmlspecialchars(json_encode($row2["ARTICLE_DESC"]), ENT_QUOTES); ?>);
Should probably include quotes around the PHP reference, so JavaScript knows what you want it to write:
child1.document.write("<?php echo htmlspecialchars(json_encode($row2['ARTICLE_DESC']), ENT_QUOTES); ?>");
Note that I changed the double-quotes surrounding ARTICLE_DESC to single-quotes as well.

Related

how to fix startWith not working in chrome

I am checking some date whether available or not from DB using Ajax using below my code,
var url = "ValidateDateFromDB.jsp?cdate="+selecteddate.value;
if (window.XMLHttpRequest)
req = new XMLHttpRequest();
else if (window.ActiveXObject)
req = new ActiveXObject("Microsoft.XMLHTTP");
req.open("GET", url, true);
req.onreadystatechange =callback;
req.send(null);
function callback()
{
if (req.readyState == 4)
{
if (req.status == 200)
{
var message = req.responseText;
alert(message);
if(message.startsWith("Available"))
{
alert("Availabe");
}
else
{
alert("Not Availabe");
}
}
}
}
I am getting output like below image,
It is working fine in firefox but not working in chrome.I checked out that startsWith works only chrome above 40 version.So to fix this i used == operation like below,
if(message=="Available")
{
alert("Availabe");
}
else
{
alert("Not Availabe");
}
But this code always goes to else part that i know why.
someone tell me how to fix this?

sending multiple data in ajax post method in jsp

function getXmlHttpRequestObject()
{
var xmlHttp = false;
if (window.XMLHttpRequest)
{
return new XMLHttpRequest(); //To support the browsers IE7+, Firefox, Chrome, Opera, Safari
}
else if(window.ActiveXObject)
{
return new ActiveXObject("Microsoft.XMLHTTP"); // For the browsers IE6, IE5
}
else
{
alert("Error due to old verion of browser upgrade your browser");
}
}
var xmlhttp = new getXmlHttpRequestObject(); //xmlhttp holds the ajax object
function servletPost()
{
if(xmlhttp)
{
var comp_to = document.getElementById("comp_to").value;
var comp_subject = document.getElementById("comp_subject").value;
var comp_letter = document.getElementById("comp_letter").value;
var date_time = document.getElementById("date_time").value;
if(comp_to==""||comp_subject==""||comp_letter==""||date_time=="")
{
document.getElementById("redSignal").style.display='block';
document.getElementById("redSignal").innerHTML="All Fields are necessary";
}
else
{
xmlhttp.open("POST","complaintHandler",true);
xmlhttp.onreadystatechange = handleServletPost;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
var data_string="to="+comp_to+"&subject="+comp_subject+"&complaint="+comp_letter+"&date_time="+date_time;
xmlhttp.send(data_string);
}
}
}
function handleServletPost()
{
if (xmlhttp.readyState == 4)
{
if(xmlhttp.status == 200)
{
document.getElementById("greenSignal").style.display='block';
document.getElementById("greenSignal").innerHTML=xmlhttp.responseText;
}
else
{
document.getElementById("redSignal").style.display='block';
document.getElementById("redSignal").innerHTML="Error Code ="+xmlhttp.status;
}
}
}
I am getting the problem of error code 404
What could be the problem in this code? Please help me.
Error 404 itself says that your URL is wrong .
xmlhttp.open("POST","complaintHandler-wrong",true);
check this URL 1st .
ERROR 404 Says. The requested Http request is not present or wrong.
Please check your "complaintHandler" It might be complaintHandler.jsp , kind of...
Please go through this tutorial for your future use.

Send Flag from PHP to JavaScript

I want to initialize a flag in a condition in PHP and send it to be read by JavaScript.
At the moment, I have this code :
PHP
if ($totalResults > MAX_RESULT_ALL_PAGES) {
$queryUrl = AMAZON_SEARCH_URL .
$searchMonthUrlParam .
$searchYearUrlParam .
$searchTypeUrlParam .
urlencode( $keyword ) .
'&page=' . $pageNum;
} else {
$queryUrl = AMAZON_TOTAL_BOOKS_COUNT .
$searchMonthUrlParam .
$searchYearUrlParam .
$searchTypeUrlParam .
urlencode($keyword) .
"&page=" . $pageNum;
$flagQuery = TRUE;
echo $flagQuery;
}
JavaScript
<script>
function getFlagValue() {
var xmlHttp;
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState==4 && xmlHttp.status==200) {
alert(xmlHttp.responseText);
}
};
xmlHttp.open("GET","getAmazonResult.php",true);
xmlHttp.send();
}
var flagQuery = new Boolean();
flagQuery = getFlagValue();
alert(flagQuery);
</script>
I can't seem to retrieve the Flag in JavaScript.
I found out why my Flag displays Undefined.
It is simply that to make a alert (flagQuery) a boolean does not function.
I combine my code with that of Jan Turon, and this is achieved
function getFlagValue() {
var xmlHttp = new HmlHttpRequest();
xmlHttp.onload = function() {
if (xmlHttp.status==200) yourCustomHandler(xmlHttp.responseText);
};
xmlHttp.open("GET","getAmazonResult.php",true);
xmlHttp.send();
}
function yourCustomHandler(response) {
flagQuery = response;
alert(flagQuery);
}
flagQuery = getFlagValue();
if (flagQuery = true) {
alert ("Flag = TRUE");
}
else {
alert ("Flag = FALSE");
}
And now I see if the flag is true or false
Just uncomment the echo $flagQuery from your PHP code and change it to
echo $flagQuery ? 1 : 0;
since false echoes nothing.
A stands for asynchronous in AJAX. Therefore in your javascript you need to set the flag var in onreadystatechange event:
function getFlagValue() {
var xmlHttp = new HmlHttpRequest();
xmlHttp.onload = function() {
if (xmlHttp.status==200) yourCustomHandler(xmlHttp.responseText);
};
xmlHttp.open("GET","getAmazonResult.php",true);
xmlHttp.send();
}
function yourCustomHandler(response) {
flagQuery = response;
alert(flagQuery);
}
Your code will also work if you make it synchronous by setting false as the third parameter in:
xmlHttp.open("GET","getAmazonResult.php",false)
and if you actualy return the responseText from the getValue() function, but I don't recommend it since the net communication may fail and you javascript then freezes.
BTW: you don't need to bother with ActiveXObject legacy code, see here. Also have a look at XHR2: I updated your JS code for XHR2 standard in my answer), as you see, it is much shorter.
Oh, and there is a minor mistake in your PHP, you need to echo the flagQuery everytime:
if ($totalResults > MAX_RESULT_ALL_PAGES) {
$queryUrl = ...
$flagQuery = FALSE;
} else {
$queryUrl = ...
$flagQuery = TRUE;
}
echo $flagQuery ? 1 : 0; // outside of the if-else block

Cant get value from json in javascript

I have a function in JavaScript:
function login() {
user=document.getElementById("user_id").value;
pass=document.getElementById("password").value;
params="user="+user+"&pass="+pass;
url="check_login.php";
if(window.XMLHttpRequest) {
var http=new XMLHttpRequest();
} else {
var http=new ActiveXObject("Microsoft.XMLHTTP");
}
var http=new XMLHttpRequest();
http.onreadystatechange=function(){
if(http.status==200 && http.readyState==4) {
text=http.responseText;
alert(text.status);
}
}
http.open("POST",url,true);
http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
http.send(params);
return false;
}
And the server side returns the json data like this:
$return=array("status"=>"true","user"=>$user,"fname"=>$fname,"middle_name"=>$middle_name,"lname"=>$lname);
$return=json_encode($return);
echo $return;
Now the alert should give the alert true but it doesn't work, I hope I am able to make you clear
Use JSON.parse(text) before your alert(text.status) for encode string in JSON object
http.onreadystatechange=function(){
if(http.status==200 && http.readyState==4) {
text=JSON.parse(http.responseText);
alert(text.status);
}
}

JavaScript mtree not working in Internet Explorer

One of my client had a form developed in component Mosets Tree. In the form when you select a main category it automatically displays the subcategories. Now the issue is; I had to hide some code to stop displaying a few things, after that the java script which was displaying subcategories after we select the main category is not working in IE.
code:
var xmlhttp;
function stateChanged(){
if (xmlhttp.readyState==4) {
document.getElementById("subCatId").innerHTML = xmlhttp.responseText;
}
}
function fnGetSubCategory() {
xmlhttp = GetXmlHttpObject();
var new_cat_id = document.getElementById("new_cat_id").value;
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return true;
}
var url="ps.php?cat_id="+new_cat_id;
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function GetXmlHttpObject() {
if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
if (window.ActiveXObject) {
return new ActiveXObject("Microsoft.XMLHTTP");
}
return null;
}
It is working fine in all other browsers.
Thanks in advance.
Try creating the object xmlHttp with this code:
function createXmlHttpRequestObject(){
var xmlHttp;
try{
xmlHttp = new XMLHttpRequest();
}
catch (e) {
// If its IE 6 or other version before
var XmlHttpVersions = new Array('MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
// We try all versions
for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++){
try {
//Try creating xmlHttp object
xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
}
catch (e){
xmlHttp = false;
}
}
}
// If object doesn't exist sends error
if (!xmlHttp){
alert("Error creating XMLHttpRequest object");
}
else{
return xmlHttp;
}
}

Categories