Uncaught Syntax error on onClick function javascript - javascript

I need to output some strings in the onclick anchor function.I have used proper escaping but anyhow its returning error.
My code is:
$output .= '<a title="Minus this" href="#" onclick = removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');></a>';
And also used this :
$output .= '<a title="Minus this" href="#" onclick = "removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ')"></a>';
But in both cases there is Uncaught SyntaxError: Unexpected token }

The quotes is totally wrong. Do this way, using the first one:
$output .= '<a title="Minus this" href="#" onclick=\'removefromCart("' . $item . '", "' . $nonce . '", ' . $deductQty . ');\'></a>';
See the 's I have added and removed spaces? And please, next time, don't mingle both PHP and JavaScript. It's confusingly dangerous.
See also:
Are single quotes allowed in HTML?
Is there a better way to write this php code?

You should use single quotations to represent string in HTML element
$output .= '<a title="Minus this" href="#" onclick = "removefromCart(\'' . $item . '\', \'' . $nonce . '\', ' . $deductQty . ')"></a>';

For greater than or equal to PHP5
$output .= '<a title="Minus this" href="#" onclick = removefromCart($item,$nonce,$deductQty);></a>';
For Less than PHP5 try it
$output .= '<a title="Minus this" href="#" onclick = removefromCart(' . $item . ',' . $nonce . ', ' . $deductQty . ');></a>';

Related

How do I add an onClick event to a link generated on PHP?

I am trying to add an onclick event on a link in a PHP file.
$body2 .= '<td class="sampling-list-td download-link">' . '<a ' . 'class="sampling-list-download" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
This is what I'm trying to do:
$body2 .= '<td class="sampling-list-download" ' . 'onClick="ga('send', 'event', { eventCategory: 'download', eventAction: 'click', eventLabel: 'Files Download'});" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';
But I am getting the following errors:
ParseError: syntax error, unexpected 'send' (T_STRING)
I don't know what i'm doing wrong? I'm trying to add the event tracking code for google analytics T_T
the quote in 'send' ends the string you started right before onClick. When the string is terminated it will interpret as PHP again and send is not a valid keyword here.
To prevent this you can escape the ' like this: 'onClick="ga(\'send\', \'event\', { e...
Its a Quote problem .change like this add\ before ' in onclick.Otherwise each time ' .is break the string and after the text are execute like php statement or variable .If the php send is invalid one so you got error
$body2 .= '<td class="sampling-list-download" ' . 'onClick="ga(\'send\', \'event\', { eventCategory: \'download\', eventAction: \'click\', eventLabel: \'Files Download\'});" ' . 'href="#" ' . 'data-id="' . $assetId . '" ' . 'data-type="' . $assetType . '" ' . 'data-event="LogFileElaboration" ' . 'data-file="' . $fileName . '" ' . 'data-enterprise="' . $provisionedBuId . '"' . '>'
. '<i class="glyphicon glyphicon-download-alt" style="color: rgb(109, 110, 113); zoom: 1.3;"></i>'
. '</a>' . '</td>';

Echo JavaScript

When I try to echo JavaScipt code with normal text like this:
echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Please enter a valid invite link!";</script>';
It works fine, but when I try to echo something that uses an user input:
echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Your New Link: ' . $link . ' (websi.te/' . $link . ')";</script>';
It doesn't work, and I get this error:
SyntaxError: unexpected token: identifier
Your quotes didn't match. I checked your code in PhpStorm, and after replacing and escaping two quotes in the JavaScript string it din't show a syntax error anymore.
I changed
"Your New Link: ' . $link . ' (websi.te/' . $link . ')"
to
\'Your New Link: ' . $link . ' (websi.te/' . $link . ')\'
complete string:
echo
'<script type="text/javascript">' .
'document.getElementById("errorbox").innerHTML = \'Your New Link: ' . $link . ' (websi.te/' . $link . ')\';' .
'</script>';
I guess the double quote is wrong.
Try to this. I edited <a href=\"...
Good luck.
echo '<script type="text/javascript">document.getElementById("errorbox").innerHTML = "Your New Link: ' . $link . ' (websi.te/' . $link . ')";</script>';

Mixing javascript with PHP? (tracking Google Analytics virtual pageviews)

I have a WordPress theme that outputs a portfolio grid based on a shortcode. If you click on the portfolio featured image, you open a modal box with the project details.
I am trying to use google's virtual pageview tracking to log a pageview when a user clicks to open the modal box.
_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);
https://developers.google.com/analytics/devguides/collection/gajs/asyncMigrationExamples
Here is how the the portfolio html is generated in my portfolio.php file:
// GENERATE HTML
$html .= '<li class="item' . $thumb_classes . '" data-title="' . get_the_title() . '">';
$html .= '<a href="' . get_the_permalink() . '"';
$html .= $enable_modal ? ' data-ajax-selector=".portfolio-detail"' : '';
$html .= ' class="item-image';
$html .= $enable_modal ? ' m-open-ajax-modal-project' : '';
$html .= '">' . $thumb_image . '</a>';
$html .= $enable_title || $enable_excerpt ? '<div class="item-info">' : '';
$html .= $enable_title ? '<h4 class="m-secondary-font"><a href="' . get_the_permalink() . '"' : '';
$html .= $enable_title && $enable_modal ? ' data-ajax-selector=".portfolio-detail" class="m-open-ajax-modal-project"' : '';
$html .= $enable_title ? '>' . get_the_title() . '</a></h4>' : '';
$html .= $enable_excerpt ? wpautop( get_the_excerpt() ) : '';
$html .= $enable_title || $enable_excerpt ? '</div>' : '';
$html .= $enable_permalink ? '<div class="item-tools"><i class="fa fa-link"></i></div>' : '';
$html .= '</li>';
I've tried adding the code below below the second line but it is causing an error:
$html .= ' onClick="_gaq.push(['_trackPageview', '/downloads/pdfs/corporateBrief.pdf']);"';
Parse error: syntax error, unexpected '_trackPageview' (T_STRING) in
/home/abc/public_html/wp-content/plugins/toolkit/shortcode-generator/shortcodes/portfolio.php
on line 234
I am assuming I should be formatting that onClick code differently but don't know where to start. From what I can tell, php doesn't like the commas ' necessary in the javascript code. Perhaps I'm way off base as well.
Any help would be appreciated!
you forgot to escape the quote mark. so php got error
$html .= ' onClick="_gaq.push([\'_trackPageview\', \'/downloads/pdfs/corporateBrief.pdf\']);"';
would work
And i think using this kind of syntax is better than using dot to contact strings.
<?php
$google = 'http://www.google.com';
?>
<li>
google
<!-- works for php5.3+ -->
goggle2
</li>
Also you may try some editor that support php code highlight, they will show you the problem

magento javascript:void(0) in navigation menu

I've been trying to add Javascript:void(0)to my magento website's navigation bar for a long time.
I read many articles about this subject and almost all of them says I should edit
/app/code/core/Mage/Catalog/Block/Navigation.php or copy this to local folder end edit there.
here one of the popular way that i followed;
To remove url, href functionality of the top menu categories, you can take the following steps:
Create some folder as this path: app/code/local/Mage/Catalog/Block
Copy file Navigation.php from app/code/core/Mage/Catalog/Block to
app/code/local/Mage/Catalog/Block
Go to function _renderCategoryMenuItemHtml()
Replace this code
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
with this code
if($category->getLevel()== 2 && $hasActiveChildren) {
$html[] = '<a href="[removed]void(0);"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
} else {
$html[] = '<a href="'.$this->getCategoryUrl($category).'"'.$linkClass.'>';
$html[] = '<span>' . $this->escapeHtml($category->getName()) . '</span>';
$html[] = '</a>';
}
But it didn't work for me. Weirdly even if i delete /app/code/core/Mage/Catalog/Block/Navigation.php the navigation menu works just fine. Were it gets the code for working I have no idea.
Maybe you have an idea that can help me. I just want to put Javascript:void(0) to the navigation menu.
I feel it's issue of cache or Magento Compiler. I suggest you to do the following steps from magento admin.
go to admin->System->Cache Management and clear/flush all the cache
go to admin->System->Tools->Compilation. Check if compilation is enabled then disabled it and clear cache again and run your compilation process
I was also looking to solve this, I think the problem is that the code you linked only works on 1.7 and not on 1.9?
The solution i came up with
copy Topmenu.php from app\code\core\Mage\Page\Block\Html to app\code\local\Mage\Page\Block\Html replace on line 131:
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
to
if ($child->hasChildren()) {
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href=\'javascript:void(0);\'><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
}
else{
$html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>'
. $this->escapeHtml($child->getName()) . '</span></a>';
}
Hope this helps.

how to add second onclick action from php

I have this line in php code:
$return .= ' <div class="acrDivP" onClick="javascript: window.location.href = \'' . $productUrl . '\';" id="acrLink">' . $resultText . '</div>';
I want to add a second onClick action as shown in bold (**) below:
$return .= ' <div class="acrDivP" onClick="javascript: window.location.href = \'' . $productUrl . '\';**ga('send', 'event', 'testEvent', 'click', 'ProductName');**" id="acrLink">' . $resultText . '</div>';
I know my syntax is horribly wrong since some charatcters have to be escaped. Could someone please help me fix the syntax? Or do I need a whole new approach?
$return .= ' <div class="acrDivP" onClick="javascript: ga(\'send\', \'event\', \'testEvent\', \'click\', \'ProductName\'); window.location.href = \'' . $productUrl . '\';" id="acrLink">' . $resultText . '</div>';
You can do it this way, it's just going to require a lot of gnarly escaping. This reduces both the readability and the maintainability of your code.
$return .= '<div class="acrDivP" onClick="window.location.href = \'' . $productUrl . '\'; ga(\'send\', \'event\', \'testEvent\', \'click\', \'ProductName\');" id="acrLink">' . $resultText . '</div>';
It would require some refactoring, but if you can break out of PHP for the code, it can be much cleaner:
<div class="acrDivP" onClick="window.location.href = '<?php echo $productUrl ?>'; ga('send', 'event', 'testEvent', 'click', 'ProductName');" id="acrLink"><?php echo $resultText ?></div>

Categories

Resources