Web Analytics Made Easy -
StatCounter How to retrieve the source code of javascript excuted from the address bar? - CodingForum


No announcement yet.

How to retrieve the source code of javascript excuted from the address bar?

  • Filter
  • Time
  • Show
Clear All
new posts

  • How to retrieve the source code of javascript excuted from the address bar?

    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!

  • #2
    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.
    Disclaimer: I'm hungover 70% of the time i'm on here, any information given may not be correct, or even legible.


    • #3
      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.
      .My new Javascript tutorial site: http://reallifejs.com/
      .Latest article: Calculators — Tiny jQuery calculator, Full-fledged OOP calculator, Big number calculator
      .Latest quick-bit: Including jQuery — Environment-aware minification and CDNs with local fallback


      • #4
        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!
        Last edited by newgnay; Sep 9, 2011, 01:42 PM. Reason: include quotation