Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile 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
    Regular Coder
    Join Date
    May 2010
    Location
    Bathurst, Australia
    Posts
    180
    Thanks
    1
    Thanked 22 Times in 22 Posts
    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
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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.

  • Users who have thanked venegal for this post:

    newgnay (09-09-2011)

  • #4
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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; 09-09-2011 at 06:42 PM. Reason: include quotation


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •