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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web