我在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>
(catchIt.php)
$thisString = $_GET['link'] ;
$page = $_GET['pg'] ;
$thisDate = date('Y-m-d H:i:s') ;
$thisIp = $_SERVER['REMOTE_ADDR'] ;
// the remained of this code has already been developed to write the details to the mySql table
// the code below is there to prove the success of this script
$thisString .= '
' . $thisDate . '
' . $thisIp . '
' . $page ;
$file = fopen("theWords.txt","w");
fwrite($file, $thisString) ;
fclose($file) ;
?>
答案 0 :(得分:0)
此行:
var theLink = encodeURIComponent(document.getElementsByClassName('theLink')) ;
在Chrome上将theLink
设置为"%5Bobject%20HTMLCollection%5D"
。如果目标是获得单击的链接的href
,则需要将该链接传递到函数中,并使用其href
属性。进行最小的更改即可做到:
<a onClick="myBasic(this, 'pagename.php');" ...
和
function myBasic(element, pgnm){
var page = pgnm ;
var theLink = encodeURIComponent(element.href);
// ...
然后,您必须发送一个同步 ajax请求(这是一个非常糟糕的主意),以确保在由于链接导致页面被撕毁之前帖子完成
您可以阻止默认操作(跟随链接),然后在ajax完成时导航到页面。
但是同步ajax和阻止默认设置都会使您误解统计信息,因为右键单击链接并在新标签页/窗口中打开它不会被记录。或者,如果有人复制了链接地址,打开了一个新窗口并粘贴它,那么该地址也将不会被记录。
从根本上讲,这种方法无法可靠地工作。您登陆的页面需要发送您登陆的信息。