Forum Moderators: open

Message Too Old, No Replies

Trying to Decipher a Simple Script

I don't understand these commands.

         

Jeremy_H

7:52 pm on May 30, 2006 (gmt 0)

10+ Year Member



Hello,

Could somebody tell me what the following script does?

To me, it seems like when there is a click in Division C, then function L is executed.

Once executed, function L sets the focus to the current window (I think) and then does something from there on.

Thanks for any help.

<script type="text/javascript">
function l(){
window.focus();
bug=new Image();
bug.src="http://www.site.com/track.php?p="+escape(document.title)+"&a="+escape(window.status);}
</script>

<div id="c" onClick="l();">....</div>

jshanman

8:46 pm on May 30, 2006 (gmt 0)

10+ Year Member



This is a logging script.

It takes advantage of the fact that when you create an Image object in javascript, and set it's src, then javascript will load that image into cache so it's available instantly.

<script type="text/javascript">
function l(){
//this is naming a global function "1"

window.focus();
//this brings this window to the forground on the client browser

bug=new Image();
//this creates an image object, though no visible picture is loaded

bug.src="http://www.site.com/track.php?p="+escape(document.title)+"&a="+escape(window.status);}
//this sets the path to the image so the browser caches the "image". What really happens is that the php page is loaded with the title of the current document and the value of the status bar (for some reason?). The php probably logs this information in a database of a log file along with the Browser,IP Address,referrer, and anything else they want.

</script>

<div id="c" onClick="l();">....</div>
this runs the function whenever the user clicks on this HTML element.

I'm not sure why they'd want the window status unless if is for debugging...

I use a similar script to log any javascript errors that a user might receive.

- JS

Jeremy_H

12:20 am on May 31, 2006 (gmt 0)

10+ Year Member



Thanks JS for your detailed reply!

If an image is being requested that doesnt exist, would this result in a 404 error? Is there a better way of doing this?

Also, is "bug" some kind of standard variable, because I don't see "var bug" being set. I tried searching for something about this on Google, but "JavaScript Bug" returns lots of unwanted results.

Thanks

jshanman

12:49 pm on May 31, 2006 (gmt 0)

10+ Year Member



If an image is being requested that doesnt exist, would this result in a 404 error?

The file path given exists, but it is not an image. As far as the browser can tell, it's an empty image. But it's actually a php file that just logged all the data given to it as GET varialbes in the URL.

Also, is "bug" some kind of standard variable, because I don't see "var bug" being set.

No. I suspect the author just didn't put var in. It's not required to use var to create a variable, but it's very helpful when setting/using local variables as opposed to global variables.

Variable scope:

var b="world";//global variable
function a() {
var b = "hello";//local (does not overwrite global)
alert(b);//alerts "hello"
}
a();
alert(b);//alerts "world"

even though the function seems like it would change the value of "b", it doesn't change the global one because "var" is used inside the function.

If you took var out of the function, then the same script would alert "hello" 2 times.

- JS

Jeremy_H

4:47 pm on May 31, 2006 (gmt 0)

10+ Year Member



Thank you very much for your great replies.

This helps me a lot!

Thanks.