0

I have created a custom module for Drupal 7. In this module, I would like it execute a javascript file that embeds an iframe when the module is viewed. I have tried the following code with no success:

function chat_with_librarian_block_view($delta = '') {

  $block = array();
  switch ($delta) {
    case 'chat_with_librarian':
      $block['subject'] = t('Ask a Librarian');
      $block['content'] = chat_with_librarian_get_html();
      break;
  }
  return $block;
}

function chat_with_librarian_get_html() {

  $html = '<div id="qpchatwidget" >' .
    '</div>' .
    '<script ...call site in question with appropriate parameters...>' .
    '<noscript>Please enable javascript to chat with librarians online</noscript></script>';
  $form = array(
    'chat_frame' => array(
      '#type' => 'markup',
      '#markup' => $html,
      '#prefix' => '<div id="parent">',
      '#suffix' => '</div>',
    )
  );
  return $form;
}

What do I need to do to get the embedded javascript to work properly. As a side note I can make this work by creating a block through the administration screen, so I know this can be done. I just need to know what I am missing in my code to accomplish the same thing. When I do execute this code, the title is rendered but the body isn't. Examining the body of the block, I can see it contains the javascipt as returned by the function and an additional message:

Reload the page to get source for: url

It seems like the executing code cannot get correct div dimensions to return a proper iframe (but that is just a guess on my part - have put inline css to define a size). Has anyone come across something like this or how to get this working?

Thanks.

0

The $form var in chat_with_librarian_get_html()is rather useless, just return the $html var instead and it should work.

function chat_with_librarian_get_html() {
  $html = '<div id="parent"><div id="qpchatwidget" ></div>' .
    '<script ...call site in question with appropriate parameters...>' .
    '<noscript>Please enable javascript to chat with librarians online</noscript></script></div>';
  return $html;
}
  • I tried this originally and it didn't work. I thought that perhaps I needed to return a form but that ended up with the same result. – user5013 Oct 21 '13 at 15:47
0

I found the solution to my problem. The reason why this wasn't working is because the div statement:

<div id="qpchatwidget" ></div>

is on one line. If you break it up into two lines:

<div id="qpchatwidget" >
</div>

Then everything works as it should. Amazing how something so simple can mess you up.

  • Can you explain why? I thought html is not sensitive to white space. – vfclists Oct 22 '13 at 0:51
  • I am not sure why this is but I have experienced this issue before in other circumstances. The morale of the story is always to keep everything on a separate line. – user5013 Oct 22 '13 at 23:20

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.