Vince P
Vince P

Reputation: 1791

Javascript command/variable based on Webpage Domain/Site

I'm using the following code for fetching a latest tweet but I would like the TWITTERUSERNAME to be different depending on the domain it is on as it's a template that is being called from one place but used across multiple domains.

<script type="text/javascript">
    jQuery('document').ready(function() {
        jQuery.getJSON('http://twitter.com/statuses/user_timeline/TWITTERUSERNAME.json?callback=?', function(data) {
             jQuery('#tweet').html(data[0].text);
        });
    });
</script>

I'm guessing just some kind of if statement in php to replace the TWITTERUSERNAME value in the script?

Any pointers on this would be great

Upvotes: 0

Views: 130

Answers (3)

Daniel
Daniel

Reputation: 11

Something like:

if($_SERVER['SERVER_NAME'] == "MyServer")
{
  $username = 'Username1';
} elseif($_SERVER['SERVER_NAME'] == "MySecondServer")
{
  $username = "Username2";
} else
{
  $username = "DefaultUsername";
}

and then you use $username as the twitterusername.

Upvotes: 0

Luke Stevenson
Luke Stevenson

Reputation: 10351

Something like:

<script type="text/javascript">
  jQuery('document').ready(function() {
<?php
  $usernames = array(
    'domain.com'      => 'twittername1' ,
    'otherdomain.com' => 'twittername2' ,
    'default'         => 'fallbacktwittername'
  );
  if( isset( $usernames[$_SERVER['SERVER_NAME']] ) ){
    $twittername = $usernames[$_SERVER['SERVER_NAME']];
  }else{
    $twittername = $usernames['default'];
  }
?>
    jQuery.getJSON('http://twitter.com/statuses/user_timeline/<?php echo $twittername; ?>.json?callback=?', function(data) {
       jQuery('#tweet').html(data[0].text);
    });
  });
</script>

Caveats/Warnings The details in the array ("domain.com","otherdomain.com") would not match any contained subdomains (so "www.domain.com" and "www.otherdomain.com" would be seen as not being the same as "domain.com" and "othersomain.com").

Upvotes: 2

Stephen
Stephen

Reputation: 3737

if you want to do this in php then you can check against the servername

<?php
if($_SERVER['SERVER_NAME'] == 'www.example.com'){
    $twit = 'username';
}
?>

then your javascript would be something like

    <script type="text/javascript">
    jQuery('document').ready(function() {
        jQuery.getJSON('http://twitter.com/statuses/user_timeline/<?=$twit?>.json?callback=?', function(data) {
             jQuery('#tweet').html(data[0].text);
        });
    });
</script>

Upvotes: 1

Related Questions