dua55014
2013-06-20 17:41
浏览 38
已采纳

将PHP代码添加到Drupal页面以添加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?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douke7431 2013-06-20 19:15
    已采纳

    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.

    打赏 评论

相关推荐 更多相似问题