homepage Welcome to WebmasterWorld Guest from 54.196.225.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
error when trying to paste url into ajax function
kpop




msg:3398546
 9:23 pm on Jul 18, 2007 (gmt 0)

Hi guy,
I'm very new to ajax. Please help me fix error below.

This code supposes to take the url from the link, put it into showHint function and output and image.


<script type="text/javascript">
var xmlHttp

function showHint(str)
{
if (str.length==0)
{
document.getElementById("txtHint").innerHTML="";
return;
}
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Your browser does not support AJAX!");
return;
}
var url="img.php";
url=url+"?img="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4)
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
</script>
<a href="#" onclick="showHint(<[small][url=http://www.WebmasterWorld.com/javascript/3346054.htm]What URL?[/url][/small]>)">here</a>
<span id="txtHint"></span>

And this is the code of img.php

<?php
echo "<img src=\"{$_GET['img']}\">";
?>

the code won't work. when I paste the source code of the page into frontpage, it said I got an error near onclick. When I replace the link of the image by a random number or any other character, it works just fine. why?

thanks.

[edited by: DrDoc at 11:13 pm (utc) on July 18, 2007]

 

lavazza




msg:3398597
 10:09 pm on Jul 18, 2007 (gmt 0)

Maybe its the &

url=url+"&sid="+Math.random();
url=url+"&amp;sid="+Math.random();

?

kpop




msg:3398611
 10:22 pm on Jul 18, 2007 (gmt 0)

lavazza, there is nothing wrong with that part.
My question is, why is it not working with links, but with simple characters and numbers?

if I was to replace: "<What URL? [WebmasterWorld.com]>" with "2", the code is working fine but of course the output is not as expected (because I didn't paste any image link to img.php

thanks.

[edited by: DrDoc at 11:13 pm (utc) on July 18, 2007]

DrDoc




msg:3398643
 11:14 pm on Jul 18, 2007 (gmt 0)

You need to put quotes around the URL.

<a href="#" onclick="showHint('http://www.example.com')">here</a>

kpop




msg:3398649
 11:17 pm on Jul 18, 2007 (gmt 0)

it worked!

thank you so much DrDoc
I didn't even notice that.

thanks.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved