2

Iam struggeling with the situation that I have to use single quotes and double quotes in Joomla $document->addScriptDeclaration.

$document->addScriptDeclaration('
    jQuery(document).ready(function () {
        jQuery("a[href="#location").on('shown', function (e) {
            initialize();
        });
    });
');

My problem here is the selector which has to be usually addressed with jQuery("a[href="#location"]). Now here is the situation with double quote in double quote. Changing to single quote is not possible, because the script declaration is within single quotes.

How can I resolve this problem?

The begin of the view is:

// No direct access
defined('_JEXEC') or die;

//JHtml::addIncludePath(JPATH_COMPONENT.'helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('behavior.keepalive'); 

$document = JFactory::getDocument();

$document->addScriptDeclaration('
   jQuery(document).ready(function () {
        // remove top banner manual because it is a module loaded by list
        jQuery("#top-content-banner").remove();
        jQuery("a[href=\'#location\']").on("shown", function (e) {
            alert("Hello");
        });
    });
');
3

You need to escape the quotes using backwards slashes like so:

// No direct access
defined('_JEXEC') or die;

//JHtml::addIncludePath(JPATH_COMPONENT.'helpers/html');
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
JHtml::_('behavior.keepalive'); 
JHtml::_('bootstrap.framework');

$document = JFactory::getDocument();

$document->addScriptDeclaration('
    jQuery(document).ready(function($) {
        $("a[href=\'#location\']").on("shown", function(e) {
            alert("it has worked");
        });
    }); 
');

Note the single quotes I have added too.

Hope this helps

  • I've tried it and copied this into the view, but it does not work. The whole part is not visible anymore. – Perino Aug 21 '14 at 11:24
  • @Perino - I've updated my code and also tested it. Works fine for me now. I also added in the closing square bracket ] which you were missing – Lodder Aug 21 '14 at 11:51
  • Now, there is no error anymore, Lodder. But the script wont be fired. It is the same issue like my comment to Faris answer. – Perino Aug 21 '14 at 12:22
  • @Perino - Have you made sure that bootstrap.js is being loaded on your page? I have honestly tested the code I've provided and it works. I added it to the template index.php but with a few modifications for me, went to go and edit an article in the frontend, and when clicking the correct tab, I got an alert message. Have you ensured you have defined $document? – Lodder Aug 21 '14 at 12:38
  • I've added the begin of the view. The first part of the scropt, the removing part will be done without problems. But the new one will only do with typical <script></script>. – Perino Aug 21 '14 at 13:23
2

You can use Heredoc string like so:

$s = <<<SCRPT
    jQuery(document).ready(function () {
        jQuery("a[href='#location']").on('shown', function (e) {
            initialize();
        });
    });
SCRPT;
$document->addScriptDeclaration($s);

Edit

You can test it yourself. It's working:

$s = <<<SCRPT
    jQuery(document).ready(function () {
        jQuery("a[href='#location']").on('click', function () {
            alert('Yes');
        });
    });
SCRPT;
$document->addScriptDeclaration($s);
echo '<a href="#location">Click Here</a>';
  • nice approach Fari, but it is not working. Ive tried it with different functions and the script approach is working. But for the selector jQuery("a[href='#location']") with single and double quotes, it is not working. – Perino Aug 21 '14 at 11:35
  • It is working. I am going to add the codes for test. The jQuery selector has no issue. – Farahmand Aug 21 '14 at 11:52
  • I have no luck. Im my case "a[href='#location']" is a bootstrap tab. The function should be fired, when the tab a[href='#location'] is clicked. If I put the script in normal <script></script> brackets, it is working. When I put into Joomlas addScriptDeclaration, the function will be not fired. – Perino Aug 21 '14 at 12:21
  • Hmm, it's weird. – Farahmand Aug 21 '14 at 12:45
  • @Fari: also your approach can be done! Problem was the same like I explained at Lodders answer. So the alert message can be displayed. But with this approach the script call initiliazie(), a separate function, won't do. – Perino Aug 21 '14 at 14:59
2

Just as another option, though I would recommend other answers over this one, you can also use output buffering to get around the quote issue:

ob_start();
// close the php tag, so this would normally just print directly to the page
// output buffering will allow us to capture it and store it in a variable
?>
jQuery(document).ready(function () {
    jQuery("a[href='#location']").on('shown', function (e) {
        initialize();
    });
});
<?php
$s = ob_get_clean();
$document->addScriptDeclaration($s);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.