Blue Cloud
Blue Cloud

Reputation: 117

Adding PHP code to Drupal Page for adding Javascript

I am trying to add some JavaScript to a Drupal Page to open a link in a popup without address bar etc. I have since learned that you can't just embed JS in the "Full HTML" view. (script tag gets commented out with HTML comments) So I have used the PHP mode of entry using the PHP function drupal_add_js(), but the php code gets commented out with HTML comments when the HTML is rendered. i.e

<?php
$myscript = 'function openWindow(){
var browser=navigator.appName;
if (browser==”Microsoft Internet Explorer”)
{
window.opener=self;

}
window.open("/page/terms-use","null","width=900,height=750,
toolbar=no,scrollbars=no,location=no,resizable =yes");
window.moveTo(0,0);
window.resizeTo(screen.width,screen.height-100);
self.close();
}'; 

drupal_add_js($myscript, 'inline'); //change the 2nd param to 'theme' if $myscript points to a theme .js file
?>

When page is viewed, it shows up in the browser as follows:

<!--?php
$myscript = 'function openWindow(){
var browser=navigator.appName;
if (browser==”Microsoft Internet Explorer”)
{
window.opener=self;

}
window.open("/page/terms-use","null","width=900,height=750,
toolbar=no,scrollbars=no,location=no,resizable =yes");
window.moveTo(0,0);
window.resizeTo(screen.width,screen.height-100);
self.close();
}'; 
drupal_add_js($myscript, 'inline'); //change the 2nd param to 'theme' if $myscript points to a theme .js file
?-->

I have used PHP Filter module to give the Page creator user access to use PHP, but still the same thing happens. Is this because of a rich text editor? But why does it happen even in PHP code mode?

Upvotes: 0

Views: 793

Answers (1)

Kirschbaum
Kirschbaum

Reputation: 46

This is more typically handled via a custom module or theme function. But assuming this is being put into a field on a node your code should work. The issue is likely a text format setting. Assuming this is Drupal 7, you can go to /admin/config/content/formats. I assume you are using PHP format. So you will want to inspect the settings for this format and make sure the PHP filter is enabled.

Upvotes: 1

Related Questions