View Full Version : How to retrieve the source code of javascript excuted from the address bar?

09-09-2011, 12:05 AM
Let me give an example:

javascript: var myvar=10; function myfunc(){}; void(0);

After excuting the above js source code in the address bar of my browser, I can use window.myvar and window.myfunc() to access myvar and myfunc. This indicates that the above "address-bar javascript code" must have been stored somewhere in the current page.

My question is: where is the "address-bar javascript code" stored in the current page? or equivalently, is it possible to retrieve the "address-bar javascript code" by using some other javascript codes?

I checked document.childNodes and document.documentElement, but cannot find the javascript source code. Maybe the source code is stored in the window object? How to retrieve it?

Thank you very much for the help!

09-09-2011, 03:17 PM
You already answered you're own question :P Stored in window object, retrieved by window.var. It's a one time use thing mostly used for developers testing/messing with things. Not to be used for anything really ever. If this question is being asked for any other reason that curiosity, I wouldn't bother. Don't rely on it for any sort of functionality for a website.

09-09-2011, 03:19 PM
The code itself isn't stored anywhere it's just executed. In your example, the code sets a global variable, and a global function, and those are stored just as any global variables and functions would be, and can be accessed as properties of the window object. The fact that there's usually some sort of reference to the executed scripts in the DOM doesn't mean anything. There are many ways to execute code (the console, the address bar, AJAX), and non of those make the code itself available as a string.

So, to reason that just because you can access the results of a piece of code, you must automatically be able to access the piece of code itself, is wrong. You can't.

09-09-2011, 07:38 PM
To BulletTimeBill:
Thank you for answering my question. I am not going to use the address bar code in my programming. I am only curious about where the "source code" excuted from the address bar is stored.

To venegal:
I see. Your answer is very clear. Thank you very much!