Hey everyone,

I just found out something interesting about views. It seems it strips out any javascript you include via the "Rewrite the output of this field".

I included this code in the rewrite area => <a href='#' onclick="alert('test working!')"> [title] </a>

The output comes up with the field title fine but I clicked and nothing happened. Using firebug, I found the link did not have my javascript. The html output is as follows:

<a href="#"> Banner1 </a>

Very sime output as expected but my javascript is completely missing.

Can someone tell me why this happens and how I may find a way around ?

Comments

WorldFallz’s picture

Not sure about why, but you should be able to work around it by theming the field, the view, or simply adding the js/jquery to the theme. I'd probably just do the latter.

mailme.eva’s picture

where do you add the js/query to the theme? particularly in zen?
thanks

mailme.eva’s picture

i just found this, which might be usefull for others stumbling over this post:
http://drupal.org/node/352970

grasmash’s picture

I would use the views custom field module:
http://drupal.org/project/views_customfield

set the input type to php and it won't try to clean it up.

sisko’s picture

. . . much appreciated

headkit’s picture

and how would you insert such a link:

<a href="javascript:callMyFunction([field_of_choice]">Click me</a>
grasmash’s picture

Create a new customfield (php) and enter:

print var_export($data, TRUE);

That will print out all of the fields in your view and show you their PHP variable identities in the $data array. Find the field you want by searching through the output.

You'll see something like $data->field_my_field = my_field_value. That will let you know that your field is referred to as $data->field_my_field.

Then you can write a php script like this:


$myfield = $data->field_my_field;
$link = "<a href='javascript:callMyFunction($myfield)'>Click me</a>";
echo $link;

acondiff’s picture

In my situation I need to use the values of other fields in my jquery.

I tried this in a PHP field:

<?php
$full_date = $data->node_data_field_event_date_field_event_date_value;
print_r($full_date);

$second = substr($full_date, -2, 2);
$minute = substr($full_date, -5, 2);
$hour = substr($full_date, -8, 2);
$day = substr($full_date, -11, 2);
$month = substr($full_date, -14, 2);
$year = substr($full_date, -19, 4);

echo "<script language=\"javascript\" type=\"text/javascript\">
  jQuery(document).ready(function() {
    $('#countdown_dashboard').countDown({
      targetDate: {
        'day': 		$day,
        'month': 	        $month,
        'year': 	        $year,
	'hour': 	        $hour,
	'min': 		$minute,
	'sec': 		$second,
      },
      'omitWeeks': true
    });
  });
</script>
";
?>

But its returning the wrong date for some reason. It is a day ahead and it displays no time. Even when I only print out the full date. What could be wrong here? Thanks!

sisko’s picture

as I may not actually end up helping - here it goes.

It's been a while since I posted the original message so I don't even remember the specifics of the original problem.

But I think I eventually resolved the problem by NOT putting my jquery code in views but instead I wrote a custome module.
The module only works when I arrive one a specific page.

Then it reads data from the page into a javascript value and begins to do whatever needs to be done.

In my experience, the javascript into views fields doesn't seem to work and it's worth exploring alternatives.

I really hope I have helped.

Imteaz Ahmed’s picture

I have built a view using view module, where I used so table from the fields of the registered user of the wepsite to see different users and their contents as a list. But the list does not show all the users but only two. I don't know what is the problem.

henrikakselsen’s picture

Then it reads data from the page into a javascript value

You have any detail on how you did this?

Henrik Akselsen | frontkom | Twitter: @FrontHenrik

plato1123’s picture

As someone eluded to by mentioning views customfield module, you can work around this issue by installing the views PHP module ( https://www.drupal.org/project/views_php ) which allows both php and javascript in the new "Global: PHP" view field it creates. The Views Php module does not rely on the php module to be turned on and allows processing of both php and javascript code. Messy, but it works.

hongpong’s picture