5

I have created a HTML anchor link. Now i need to convert this link to Drupal's link using l() function. But i am not getting what i want. I am new to this Drupal environment.

HTML Link :

<a href="#" onclick="loadQtiPlayer('.$targetPath.','.$targetWindowHeight.','.$targetWindowWidth.')" id="qtiStatQuiz1">'.START_QTIQUIZ.'</a>

What i have tried is,

l('Start QTI Quiz', '#', array('','onclick' => "loadQtiPlayer('.$targetPath.','.$targetWindowHeight.','.$targetWindowWidth.')"));

This is not working. I am using Drupal 6.14.

Can anyone point out where is the issue?

3 Answers 3

3
$current_path = check_url(drupal_get_path_alias($_GET['q']));
$onclick = 'loadQtiPlayer(' . $targetPath . ',' . $targetWindowHeight . ',' . $targetWindowWidth . ')';
print l('Start QTI Quiz', $current_path, array('fragment' => ' ', 'attributes' => array('onclick' => $onclick)));
4
  • EDIT: added filter_xss instead of check_url... Not sure which is more safe/efficient in this case. :/
    – jerdiggity
    Apr 22, 2013 at 8:37
  • Wow.. Works like a charm. Thank you man for this awesome answer.
    – Edwin Alex
    Apr 22, 2013 at 8:42
  • 1
    Use check_url(), filter_xss() is for escaping HTML from content that will be displayed on the screen.
    – rooby
    Apr 25, 2013 at 5:16
  • @rooby Thanks... I figured as much, if for no other reason than less resource-intensive. In fact that's how I originally had it posted, but-- well the "but" is not important... Changing back. :)
    – jerdiggity
    Apr 25, 2013 at 7:39
2

You need to use the fragment option to add a fragment to the URL. l() makes it pretty difficult to get an empty fragment, but you can get very close to it with a little hack:

$link = l('Start QTI Quiz', '', array('fragment' => ' ', 'external' => TRUE)));

That'll do the trick for Drupal 6 & 7.

0

In this case as the href is pointing at any content there's no point in using the l() function at all. You may as well code the A tag directly as it's not really a link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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