0
<script>

   $(document).ready(function() {   
   $(".tweets").liveTweets({operator: "#google"});
            });
</script>

I like to make the #google as a variable, so that I can change the ticker symbol as needed. I tried to echo with php. But, it's breaking the live tweet jquery.

Thanks!

1
  • You want to pass '#google' as a variable into the liveTweets() object? Or you want to use PHP to echo a variable into the liveTweets() object? Do you want to do this server-side or client-side? Commented Apr 20, 2013 at 22:49

2 Answers 2

1
<?php
  $operator = "#google";
?>

<script>

   $(document).ready(function() {   
   $(".tweets").liveTweets({operator: <?php echo json_encode($operator)?>});
            });
</script>

EDIT: Thank you for your comment @icktoofay.

He is right, we don't need to wrap output in double quotes, it does that for us. I've just updated it.

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

4 Comments

Don't build JavaScript with server side code. It's a bad design decision.
Of course it's bad practice. But If OP is trying to pass variable from server side on page load, that's one of the way to do this. Let's know what is your's?
json_encode already adds quotes; adding a pair around the call to json_encode will just generate invalid JavaScript like ""hello"".
Thanks guys! I am not building a super site. So used the jason_encode and worked great. FYI... I am trying to integrate this plugin in my financial website. codecanyon.net/item/live-tweets-jquery-plugin/… .. Now I need to see how to pass other variables.
0

HTML belongs in .html files, CSS belongs in .css files, and JS belongs in .js files. If you want to pass additional data from the server (PHP in this case) to JavaScript, you should use [data-*] attributes.

Wherever you're defining your .tweets element, you should pass the value you want used for operator as a custom data-* attribute:

HTML:
<?php
    $operator = '#google';
?>
<div class="tweets" data-operator="<?php echo htmlspecialchars($operator) ?>">
    ...contents...
</div>

In your separate JS file, you can then use the custom server variable when initializing the widget:

JS:
$('.tweets').each(function () {
    $(this).liveTweets({
        operator: $(this).data('operator')
    });
});

Alternatively, you can use the data-* attribute to contain the entire JSON object to initialize the widget:

HTML:
<?php
    $liveTweets = array(
        'operator' => '#google'
    );
?>
<div class="tweets" data-live-tweets="<?php echo htmlspecialchars(json_encode($liveTweets)) ?>">
    ...contents...
</div>
JS:
$('.tweets').each(function () {
    $(this).liveTweets($(this).data('liveTweets'));
});

Also, you could select elements based on the condition of having the appropriate data-* attribute:

JS:
$('[data-live-tweets]').each(function () {
    $(this).liveTweets($(this).data('liveTweets'));
});

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.