简体   繁体   English

将PHP代码添加到Drupal页面以添加Javascript

[英]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. 我试图将一些JavaScript添加到Drupal页面中,以在没有地址栏等的弹出窗口中打开链接。此后,我得知您不能仅将JS嵌入“完整HTML”视图中。 (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. (脚本标记使用HTML注释注释掉),因此我使用了PHP函数drupal_add_js()使用PHP进入模式,但是在呈现HTML时,PHP代码使用HTML注释注释掉了。 ie

<?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. 我已经使用PHP过滤器模块向页面创建者用户授予使用PHP的访问权限,但是仍然发生了同样的事情。 Is this because of a rich text editor? 这是由于富文本编辑器吗? But why does it happen even in PHP code mode? 但是,为什么即使在PHP代码模式下也会发生这种情况?

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. 假设这是Drupal 7,则可以转到/ admin / config / content / formats。 I assume you are using PHP format. 我假设您使用的是PHP格式。 So you will want to inspect the settings for this format and make sure the PHP filter is enabled. 因此,您将需要检查此格式的设置,并确保启用了PHP过滤器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM