如果在浏览器不支持JavaScript时单击链接后如何执行脚本? [英] How to execute a script after a link is clicked, when browser does not support JavaScript?

查看:83
本文介绍了如果在浏览器不支持JavaScript时单击链接后如何执行脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用下面的Facebook转换代码,包括

脚本和noscripts标签:

I am trying to use Facebook conversion code like below that includes
both scripts and noscripts tags:

<html>
    <head>
      <script type="text/javascript">

          // Wait for the page to load first
          window.onload = function() {

          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById("mylink");

          //Set code to run when the link is clicked
          // by assigning a function to "onclick"
              a.onclick = function() {
                 // Code to be executed when the link is clicked.
                 var _fbq = window._fbq || (window._fbq = []);
                 ...
              }
            }
          </script>
          <noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=6030151253043&amp;cd[value]=0.00&amp;cd[currency]=USD&amp;noscript=1" /></noscript>
    </head>
    <body>
        <a id="mylink" href="http://example.com">Link</a>
    </body>
</html>

通常我可以将此代码放在我网站的感谢页面中。但现在我有一种情况,我想跟踪是否有人点击外部链接导致某些外部网页无法控制。我的页面在加载时不应该执行此转换代码,而是仅在单击链接时执行。

Usually I can put this code in a thank you page in my website. But now I have a situation that I want to track if someone clicks an external link which leads to some external webpage that is out of my control. My page should not execute this conversion code when it loads but only when the link is clicked.

例如,我的页面有一个链接转到 http://example.com ,因此当用户点击此链接时,可以执行此转换代码,然后转到 http://example.com

For example, my page has a link that goes to http://example.com, so when a user clicks this link, this conversion code can be executed and then go to http://example.com?

问题是当浏览器不支持时JavaScript,即使没有点击链接,noscript标签内的代码也会执行。

The problem is that when the browser does not support JavaScript, the code inside noscript tag will execute even if the link was not clicked.

有没有办法解决这个问题?

Is there any way to solve that problem?

推荐答案

这是如何使用链接来调用JavaScript?


Here is an edited version of the answer on How to use a link to call JavaScript?

        // Wait for the page to load first
        window.onload = function() {

          //Get a reference to the link on the page
          // with an id of "mylink"
          var a = document.getElementById("mylink");

          //Set code to run when the link is clicked
          // by assigning a function to "onclick"
          a.onclick = function() {

            // Your code here...

            //If you don't want the link to actually 
            // redirect the browser to another page,
            // "google.com" in our example here, then
            // return false at the end of this block.
            // Note that this also prevents event bubbling,
            // which is probably what we want here, but won't 
            // always be the case.
            return false;
          }
        }
    </script>
    <noscript><img height="1" width="1" alt="" style="display:none" src="https://www.facebook.com/tr?ev=6030151253043&amp;cd[value]=0.00&amp;cd[currency]=USD&amp;noscript=1" /></noscript>
</head>
<body>
    <a id="mylink" href="http://example.com">Link</a>        
</body>
</html>

noscript 标记。这就是为什么你不打算在那里放任何JavaScript。

noscript tag will be used only if the browser does not support JavaScript. And that's why you are not going to put any JavaScript there.

这篇关于如果在浏览器不支持JavaScript时单击链接后如何执行脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆