Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Firefox中使用简单Javascript_Javascript - Fatal编程技术网

无法在Firefox中使用简单Javascript

无法在Firefox中使用简单Javascript,javascript,Javascript,我对PHP、MySQL、HTML和CSS有相当丰富的经验,但我在学习Javascript的早期阶段遇到了困难。我有一个脚本,它在IE、Opera和Chrome中的功能与预期一致,但在Firefox中没有 下面是两个相关的脚本。这是我在这里的第一篇帖子,如果我做得不对,我道歉。尽管如此,我们还是希望得到指导 <html> <!-- The purpose of this script is to to capture the details of the hotlink that

我对PHP、MySQL、HTML和CSS有相当丰富的经验,但我在学习Javascript的早期阶段遇到了困难。我有一个脚本,它在IE、Opera和Chrome中的功能与预期一致,但在Firefox中没有

下面是两个相关的脚本。这是我在这里的第一篇帖子,如果我做得不对,我道歉。尽管如此,我们还是希望得到指导

<html>
<!-- The purpose of this script is to to capture the details of the hotlink that was clicked without affecting the visible hotlink destination -->
<!-- This script works in IE, Chrome and Opera, but not Firefox -->

<head>
<title>Dev</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script src="jQuery.js"></script>

<script>
function myBasic(pgnm){
        var page = pgnm ;
        var theLink = encodeURIComponent(document.getElementsByClassName('theLink')) ;
        // the alert window displays the correct data
        window.alert(theLink + "\n" + page);
        jQuery.post("catchIt.php?link=" + theLink + "&pg=" + page) ;
        return true ;
    }   
</script> 

</head>

<body bgcolor="#FFFFFF" text="#000000">

<!-- This is a series of many hotlinked URLs that need to be recorded and stored in a mySql table -->
<!-- The pagename and hotlinks will be inserted via a PHP script -->

  <p><a onClick="myBasic('pagename.php');" href="destination.php" class="theLink">(Not) Working script</a></p>
  <p><a onClick="myBasic('anotherPagename.php');" href="http://example.com/destination.php?a=1&b=2" class="theLink">Another (Not) Working script</a></p>

</body>
</html>
这一行:

var theLink = encodeURIComponent(document.getElementsByClassName('theLink')) ;
在Chrome上将链接设置为
%5Bobject%20HTMLCollection%5D”
。如果目标是获取所单击链接的
href
,则需要将链接传递到函数中,并使用其
href
属性。为此进行的最小更改:

<a onClick="myBasic(this, 'pagename.php');" ...
然后,您必须发送一个同步ajax请求(这是一个非常糟糕的主意),以确保在页面因为跟随链接而被删除之前完成文章

您可以阻止默认操作(在链接之后),然后在ajax完成时导航到页面

但是同步ajax和防止默认设置都会给您带来误导性的统计信息,因为右键单击链接并在新选项卡/窗口中打开它不会被记录。或者,如果有人复制了链接地址,打开一个新窗口并粘贴了它,也不会被记录下来


从根本上说,这种方法无法可靠地工作。您登录的页面需要发送您登录的信息。

Firefox到底出了什么问题?你能解释一下这个问题吗?
var theLink=encodeURIComponent(document.getElementsByClassName('theLink')没有多大意义。您依赖于将HTMLCollection隐式转换为字符串。(在Chrome上,这将返回字符串
%5Bobject%20HTMLCollection%5D”
)这行的目的是什么?恕我直言,我不相信问题中的代码“…在IE、Opera和Chrome中的预期功能…”您正在启动异步ajax以响应链接单击,试图发送
%5Bobject%20HTMLCollection%5D”
转到PHP页面(但它不会到达那里,因为发起请求的页面在请求真正开始之前就被拆掉了,因为它会到达目标页面。如果您试图将所单击链接的详细信息发布到服务器,使用当前代码,您将在AJAX请求完成和浏览器之间存在竞争条件。)卸载当前页面。感谢大家的及时评论。我几乎完全走错了方向,这对我来说并不奇怪!;-)TJC,是的,正如我所说的,它在浏览器中不起作用……但是它确实起作用了,所以我有点不明白为什么(可能是某种缓存问题)。我唯一要注意的是(根据我所读的内容)onClick将发生,然后“return true”将允许转发到href。感谢对代码的建议修改-我将进行实验。显然,这里需要进一步(深入)思考,但如果可以的话,我将在一两天内回来。
<a onClick="myBasic(this, 'pagename.php');" ...
function myBasic(element, pgnm){
    var page = pgnm ;
    var theLink = encodeURIComponent(element.href);
    // ...