Solved

call javascript inside javascript var

Posted on 2010-09-01
6
241 Views
Last Modified: 2012-08-01
I have the following script and want modifiy it so that the first script is only called when the tweeter onclick function fired.....I do not want to run the first script onload, just want to run it when onclick on tweeter button.. Is this possible ?



<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=tweettrackjs&apiKey=R_7e9987b2fd13d7e4e881f9cbb168f523"></script>

<script type='text/javascript'>
//<!--
var TweetAndTrack = {
      open: function(targ, url, msg) {
          var callback_name = url.replace(/\W/g, '');
          BitlyCB[callback_name] = function(data) {
              var result = TweetAndTrack.popResult(data);
              var tweet_url = "http://twitter.com/home?status=" + encodeURIComponent( (msg ? msg + " " : '')+ result.shortUrl);        
              document.location.href=tweet_url
              
          };
          BitlyClient.call('shorten', {'longUrl': url, 'history': '1'}, 'BitlyCB.' + callback_name);
          return false;
      },
      popResult: function(data) {
          // Results are keyed by longUrl, so we need to grab the first one.
          for (var r in data.results) {
              return data.results[r];
          }
      }
}
//-->
</script>
<div id="social_wrapper">
<div id="socialicons" class='share'>
<a href='<?php echo $stumble ?>' title='Share with Stumbleupon' target="_blank" rel="nofollow" class="stumble">
<!--img src='/images/modules/socialnetwork/stumbleupon.gif' width='16px' height='16px' alt='Sumble Upon' /-->
</a>
<a href='<?php echo $digg ?>' title='Share with Digg'  target="_blank" rel="nofollow" class="digg">
<!--img src='/images/modules/socialnetwork/digg.gif' width='16px' height='16px' alt='Digg' /-->
</a>
<a href='<?php echo $facebook ?>' title='Share with Facebook'  target="_blank" rel="nofollow" class="facebook">
<!--img src='/images/modules/socialnetwork/facebook.gif' width='16px' height='16px' alt='Facebook' /-->
</a>
<a href="<?php echo $twitter ?>" onclick="return TweetAndTrack.open(this, '<?php echo JURI::current() ?>','I\'m currently loving this page on Blue Chip Vacations');" class="twitter">
<!--img src='/images/modules/socialnetwork/twitter.gif' width='16px' height='16px' alt='Twitter' /-->
</a>
<a href="<?php echo $email ?>" onclick="window.open(this.href,'win2','width=400,height=350,menubar=yes,resizable=yes'); return false;" class="email">
<!--img src='/images/modules/socialnetwork/email.png' width='16px' height='16px' alt='Email' /-->
</a>
</div>
</div>

</div>
0
Comment
Question by:bcv2010
  • 3
  • 3
6 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 33578580
Try this :

      var TweetAndTrack = null;
      function TandA() {
            TweetAndTrack = {

And :

< a href="< ?php echo $twitter ? >" onclick="TandA();return TweetAndTrack.open(this, '< ?php echo JURI::current() ? >','I\'m currently loving this page on Blue Chip Vacations');" class="twitter" >
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=tweettrackjs&apiKey=R_7e9987b2fd13d7e4e881f9cbb168f523"></script>
<script type='text/javascript'>
//<!--
	var TweetAndTrack = null;
	function TandA() {
		TweetAndTrack = {
			open: function(targ, url, msg) {
				var callback_name = url.replace(/\W/g, '');
				BitlyCB[callback_name] = function(data) {
					var result = TweetAndTrack.popResult(data);
					var tweet_url = "http://twitter.com/home?status=" + encodeURIComponent( (msg ? msg + " " : '')+ result.shortUrl);        
					document.location.href=tweet_url
				};
				BitlyClient.call('shorten', {'longUrl': url, 'history': '1'}, 'BitlyCB.' + callback_name);
				return false;
			},
			popResult: function(data) {
			// Results are keyed by longUrl, so we need to grab the first one.
				for (var r in data.results) {
					return data.results[r];
				}
			}
		}
	}
//-->
</script>
</head>
<body>
<div id="social_wrapper">
    <div id="socialicons" class='share'>
        <a href='<?php echo $stumble ?>' title='Share with Stumbleupon' target="_blank" rel="nofollow" class="stumble">
            <!--img src='/images/modules/socialnetwork/stumbleupon.gif' width='16px' height='16px' alt='Sumble Upon' /-->
        </a>
        
        <a href='<?php echo $digg ?>' title='Share with Digg'  target="_blank" rel="nofollow" class="digg">
            <!--img src='/images/modules/socialnetwork/digg.gif' width='16px' height='16px' alt='Digg' /-->
        </a>
        
        <a href='<?php echo $facebook ?>' title='Share with Facebook'  target="_blank" rel="nofollow" class="facebook">
            <!--img src='/images/modules/socialnetwork/facebook.gif' width='16px' height='16px' alt='Facebook' /-->
        </a>
        
        <a href="<?php echo $twitter ?>" onclick="TandA();return TweetAndTrack.open(this, '<?php echo JURI::current() ?>','I\'m currently loving this page on Blue Chip Vacations');" class="twitter">
            <!--img src='/images/modules/socialnetwork/twitter.gif' width='16px' height='16px' alt='Twitter' /-->
        </a>
        
        <a href="<?php echo $email ?>" onclick="window.open(this.href,'win2','width=400,height=350,menubar=yes,resizable=yes'); return false;" class="email">
            <!--img src='/images/modules/socialnetwork/email.png' width='16px' height='16px' alt='Email' /-->
        </a>
    </div>
</div>
</body>
</html>

Open in new window

0
 

Author Comment

by:bcv2010
ID: 33582749
Not sure what the code is about but I want the following code NOT to appear on page load, and want it appear when onclick

<script type="text/javascript" charset="utf-8" src="http://bit.ly/javascript-api.js?version=latest&login=tweettrackjs&apiKey=R_7e9987b2fd13d7e4e881f9cbb168f523"></script>
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 33583351
>I want the following code NOT to appear on page load, and want it appear when onclick

so remove it and use this to add the script dynamically :


function addScript() {
		var script = document.createElement("script");
		script.setAttribute("type","text/javascript");
		script.setAttribute("charset", "utf-8");
		script.setAttribute("src", "http://bit.ly/javascript-api.js?version=latest&login=tweettrackjs&apiKey=R_7e9987b2fd13d7e4e881f9cbb168f523");
		document.getElementsByTagName("head")[0].appendChild(script);
}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bcv2010
ID: 33585324
what about the closing script ?  

the </script>
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 33585392
there's no opening : < script >
we add the script dynamically :
0
 

Author Comment

by:bcv2010
ID: 33761773
ok i will test it and let you know...
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now