Mysterious URL variable inside built-in event handlers

In the following code, what will you see in the alert dialog when you click , click here ?

<script>
URL = 'hello' ;
document.write(URL) ;
</script>
<div onclick="alert(URL)">click here</div>

Fiddle

I was so sure that the answer would be “hi” that I didn’t even want to try it. But actually I see the URL of the current page (tested in Chrome, Firefox, and IE9).

Why is this URLvaraible defined inside the built-in event handlers?
Why does this take precedence over my variable?
Where can I find documentation about this behavior?


Subsequent

Is this intended only inside the built-in event handlers?
What is the motivation for something like that?


Follow-up 2

, . :

  • Mozilla 1.0.1
  • Firefox 2.0.0.6
  • IExplorer 6.0
  • Nestscape 8.1.2
  • Opera 9.02

, URL window.URL/. .

+4
1

window.URL ( URL) . ( script , URL, , .) URL . , , URL, .

, . URL. script, ( ) window.URL, . ( , window.URL, script , var n = new URL(...); , .)

, <div>, onclick :

function() {
    alert(URL);
}

, ) , document. , URL document.URL. ( document.URL .) , :

<script>
URL = 'hello' ;
document.write(URL);
</script>
<div onclick="alert(URL === document.URL)">click here</div>
+7

All Articles