最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Multiple lines in onClick? - Stack Overflow

programmeradmin4浏览0评论

I have a link and I need it to do two thing when it's clicked. First, it tracks the h it using the google tracking code, and the next is that it opens a new window.

I tried:

onclick="javascript: pageTracker._trackPageview('/Ads/MMA_Front_Page'); window.open(this.href, '_blank'); return false;"

But it didn't work, it didn't open a new window.

Did I do something wrong with the code or do I need to create a javascript method and call that?

Thanks

Edit: I am trying with functions, but it wont work. Can you help me please?

Here is the function:

<script type="text/javascript">
    function openAd(adType) {
        pageTracker._trackPageview(adType);
        window.open(this.href, '_blank');
        return false;
    }
</script>

And the part where i call it:

onclick="openAd('/Ads/MMA_Front_Page')"

It seems only the first part is working "pageTracker._trackPageview(adType);" and the rest is being ignored.

I have a link and I need it to do two thing when it's clicked. First, it tracks the h it using the google tracking code, and the next is that it opens a new window.

I tried:

onclick="javascript: pageTracker._trackPageview('/Ads/MMA_Front_Page.'); window.open(this.href, '_blank'); return false;"

But it didn't work, it didn't open a new window.

Did I do something wrong with the code or do I need to create a javascript method and call that?

Thanks

Edit: I am trying with functions, but it wont work. Can you help me please?

Here is the function:

<script type="text/javascript">
    function openAd(adType) {
        pageTracker._trackPageview(adType);
        window.open(this.href, '_blank');
        return false;
    }
</script>

And the part where i call it:

onclick="openAd('/Ads/MMA_Front_Page.')"

It seems only the first part is working "pageTracker._trackPageview(adType);" and the rest is being ignored.

Share Improve this question edited Dec 10, 2011 at 21:14 TheGateKeeper asked Dec 10, 2011 at 18:10 TheGateKeeperTheGateKeeper 4,53023 gold badges68 silver badges105 bronze badges 1
  • 4 What's wrong with creating functions inside your <script> tag? – César Commented Dec 10, 2011 at 18:11
Add a ment  | 

2 Answers 2

Reset to default 3

You should really use functions inside your <script> tag but, try without the javascript:... like:

onclick="pageTracker._trackPageview('/Ads/MMA_Front_Page.'); window.open(this.href, '_blank'); return false;"
onclick="do_something();"


<script type="text/javascript">
    function do_something(){
       pageTracker._trackPageview('/Ads/MMA_Front_Page.'); 
       window.open(this.href, '_blank'); 
       return false;
    }
</script>

You have included answer in your question. It will be better to create a function for multiple statement.

发布评论

评论列表(0)

  1. 暂无评论