minou
minou

Reputation: 16563

Insert javascript widget on one page of Drupal site

I have a javascript widget that can be inserted on an a plain-old html page like this:

<html>
    <head>
        <script type='text/javascript' src="http://example.com/widget.js"></script>
    </head>
    <body>
        <script type='text/javascript'>
            //<![CDATA[            
            try{ widget_constructor('key',500,400); }
            catch(e){ alert(e.message); }
            //]]>
        </script>
    </body>
</html>

I would like to insert this javascript on one page of a Drupal 6 site (not every page).

This is what I have tried so far to get the script tag in the HEAD:

I then added this to the top of the body of my Drupal page:

<?php 
  drupal_set_html_head('<script type="text/javascript" src="http://example.com/widget.js"> </script>'); 
?>

But Drupal doesn't parse the php and instead prints this at the top of the page:

<?php drupal_set_html_head(''); ?> 

Any suggestions as to how I can do this?

Upvotes: 2

Views: 3002

Answers (3)

s.Daniel
s.Daniel

Reputation: 1064

@Jeff: You should not really reconsider that. Using the PHP Filter is generally a bad idea as it potentially opens security holes. (In case someone would gain access to your user account or you have a misconfiguration in your settings this is a direct path to compromising the whole server)

I would rather propose to add js via the template.php of your theme. Also the right method to use is drupal_add_js with the option inline: http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_add_js/6

Here are some further reads on how to use it:

http://drupal.org/node/304178#comment-1000798 http://drupal.org/node/482542

Upvotes: 3

minou
minou

Reputation: 16563

After writing that all out, I figured out the problem...

The full html input format has an HTML corrector filter turned on by default, and that filter appears to have caused problems with the PHP code. Reordering the filters to put the PHP evaluator first fixed the problem.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270609

I'm not too familiar with Drupal so this answer merely gets around your widget not loading without actually answering the question why that PHP block isn't being parsed as PHP.

You don't need to call the <script> tag inside the <head>. You can just place that line right above the <script> tag that calls the widget constructor.

<html>
    <head>

    </head>
    <body>
        <script type='text/javascript' src="http://example.com/widget.js"></script>
        <script type='text/javascript'>
            //<![CDATA[            
            try{ widget_constructor('key',500,400); }
            catch(e){ alert(e.message); }
            //]]>
        </script>
    </body>
</html>

Upvotes: 2

Related Questions