开发者

Inserting jQuery with Drupal 7

开发者 https://www.devze.com 2023-02-10 13:48 出处:网络
I want to use the Colourbox jQuery script on my Drupal 7 site, but I only want it to load on certain content types.

I want to use the Colourbox jQuery script on my Drupal 7 site, but I only want it to load on certain content types.

I found [this script][1] that does want I want. It is for Drupal 6, but seems to work perfectly for Drupal 7.

In the script, you have to insert the Java Script inside a PHP wrapper. However, because my Java Script uses two kinds of quotation marks/apostrophes it breaks the script. How can I correct this?

Here is the script I am trying to use (inserted in template.php):

function THEMENAME_preprocess_node(&$variables) { // test for resource page node type if ($variables['type'] == 'resources_page') {

// include colorbox javascript
drupal_add_js(path_to_theme() . '/scripts/colorbox/jquery.colorbox.js');


// include colorbox css
drupal_add_css(path_to_theme() . '/scripts/colorbox/colorbox.css');  
   $js = '        jQuery(document).ready(function(){
      jQuery('.photos>a').attr('rel',

'colorbox'); jQuery("a[rel='colorbox']").each(function(){

jQuery(this).colorbox({title:jQuery(this).attr('title')+

" " + jQuery(this).find('img').attr('alt')}); }); '; drupal_add_js($js, 'inline'); } }

The bit jQuery("a[rel='colorbo开发者_JS百科x']"). breaks the script as it has two kinds of quotation marks. How can I fix this?

[1]: // Add jScript http://thedrupalblog.com/adding-jquery-image-carousel-your-node-view


I suppose you could make use of the heredoc feature of PHP, but actually, I would find it more sensible to put your custom JavaScript in its own file and include it with drupal_add_js. Just like you did with the colorbox plugin.

drupal_add_js('/path/to/your/custom.js/');

I think most web applications adhere to the concept of Separation of Concerns, that is, separating markup (HTML) from style (CSS) and behaviour (JavaScript) for example. It could ease maintenance, say you have designers on your team that are responsible for HTML/CSS/JS only, you probably don't want them to touch PHP code.

And of course you could escape the quotes.

$foo = 'Hello \'Name\'';
$bar = "$('a[rel=\"colorbox\"]')";
0

精彩评论

暂无评论...
验证码 换一张
取 消