最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript function not get called when arguments from php - Stack Overflow

programmeradmin1浏览0评论

I am running the < a > tag in php. Whenever I pass an argument in js function it does not get called but if i pass empty arguments, the function gets called.

js:

function displayBigImage(img){
  alert("inside func");
}

php:

//NOT WORKING:
echo "<a href='javascript:displayBigImage('".$row['IMG_ID']."')'>Press</a>";

//WORKING:
echo "<a href='javascript:displayBigImage()'>Press</a>";

I also tried with harcode argument values like,

echo "<a href='javascript:displayBigImage('sample.jpg')'>Press</a>";

or

echo "<a href='javascript:displayBigImage(sample.jpg)'>Press</a>";

I don't understand whats wrong?!?!?!?!

PLease reply asap.

Thanks in advance

I am running the < a > tag in php. Whenever I pass an argument in js function it does not get called but if i pass empty arguments, the function gets called.

js:

function displayBigImage(img){
  alert("inside func");
}

php:

//NOT WORKING:
echo "<a href='javascript:displayBigImage('".$row['IMG_ID']."')'>Press</a>";

//WORKING:
echo "<a href='javascript:displayBigImage()'>Press</a>";

I also tried with harcode argument values like,

echo "<a href='javascript:displayBigImage('sample.jpg')'>Press</a>";

or

echo "<a href='javascript:displayBigImage(sample.jpg)'>Press</a>";

I don't understand whats wrong?!?!?!?!

PLease reply asap.

Thanks in advance

Share Improve this question edited Aug 22, 2009 at 21:43 Gumbo 656k112 gold badges791 silver badges851 bronze badges asked Aug 22, 2009 at 21:39 sbsb
Add a ment  | 

3 Answers 3

Reset to default 8

You have problems with your quoting:

<a href='javascript:displayBigImage('sample.jpg')'>

You can't use single quotes both around the HTML attribute and within it. You need to use different quotes in the two places, for instance:

<a href="javascript:displayBigImage('sample.jpg')">

So in your PHP, that bees:

echo "<a href=\"javascript:displayBigImage('".$row['IMG_ID']."')\">Press</a>";

You have some mismatched quote marks. Where you have this:

echo " < a href='javascript:displayBigImage('".$row['IMG_ID']."')'>Press< / a >";

You should have this:

echo " <a href=\"javascript:displayBigImage('" . $row['IMG_ID'] . "')\">Press</a>";

If you’re using single quotes for the HTML attribute value declaration, you can’t use the same quotes inside the attribute values without describing them by character references.

So you either use the double quotes inside your href attribute value:

echo "<a href='javascript:displayBigImage(\"".$row['IMG_ID']."\")'>Press</a>";

Or you use proper character references:

echo "<a href='javascript:displayBigImage(&#27;".$row['IMG_ID']."&#27;)'>Press</a>";

Or you use the double quotes for the href attribute value declaration:

echo "<a href=\"javascript:displayBigImage('".$row['IMG_ID']."')\">Press</a>";
发布评论

评论列表(0)

  1. 暂无评论