0

I want to pass variables to a js function:

<script>

(function(d, s, id) {
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) return;
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/de_DE/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

// async init once loading is done
window.fbAsyncInit = function() {
    FB.init({appId: 1234567890987, status: false});
};

function gogogo(data) {

    FB.ui({
        method: 'feed',
        link: data.url,
        picture: data.picture,
        name: data.title,
        //caption: data.caption,
        description: data.description
    });
}

Gets in here and displayed all except description:

<%= link_to image_tag('icons/social-fb.png'),'#', :onclick=>"gogogo({ url:'#{request.original_url}',title:'#{content.title}',picture:'#{PICTURE_PATH+content.tg_medias.first.media.url(:big)}', description:'#{content.description}' }) ;return false;" %>

content.description is not working.. Thanks

1
  • Please don't update your question to remove the original problem. It makes my answer irrelevant and removes the resource from others who may have the same problem in the future. Please accept my answer if it solves your original problem, and open a new question now that you're having a different problem. Commented Feb 24, 2017 at 15:29

2 Answers 2

1

You should pass a javascript object as the argument to the gogogo function. Try replacing

gogogo(data=(url:'#{request.original_url}',url:'#{content.title}'));

with

gogogo({url:'#{request.original_url}',title:'#{content.title}'});

I'm assuming the fact that your argument's two keys were both url was a typo.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks.. yes a typo..Now i have: <%= link_to image_tag('icons/social-fb.png'),'#', :onclick=>"gogogo({ url:'#{request.original_url}',title:'#{content.title}',caption:'#{content.title}', picture:'#{PICTURE_PATH+content.tg_medias.first.media.url(:big)}', desc:'#{sanitize(content.description)}' });return false;" %> . All is working, but not content.description. It seems that text must be handled different..or html tags may be not allowed..
Sorry, I don't know ruby, so I might be of limited use. What shows up in the browser as the desc value? i.e. what does ruby return for #{sanitize(content.description)}?
description p.a.: <p>hello world</p>, sanitize makes sure that there is no 'bad code' in it..when I supply this value the popup does not open and I get: Uncaught SyntaxError: Invalid or unexpected token in the console
As long as the string is quoted in the javascript object, that's a perfectly acceptable value (html in an object value is no problem). Can you please post the full generated html markup of the tag? There must be something invalid in there.
The current code ist displayed on the top...Thanks for checking
|
0

I have done this in my many projects. So I will suggest you following approach.

  • Put an id on the link
  • add an script tag in the view

Add following code in a script tag in your view

$(document).ready(function($){
    $("a#id_of_that_link").on('click', function(e){
        gogogo(#{@prepared_data.to_json}, true, #{@anything_else.to_json});
    });
});

Now you will get these params at your function. Let me know If you have any confusion.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.