Explicit way to check whether a function was called from within the WindowIs there a better way to do...

Lubuntu 18.10 File Manager: How to view directory tree structure?

Coworker is trying to get me to sign his petition to run for office. How to decline politely?

Is it possible to narrate a novel in a faux-historical style without alienating the reader?

Can you say "leftside right"?

How can guns be countered by melee combat without raw-ability or exceptional explanations?

Was Opportunity's last message to Earth "My battery is low and it's getting dark"?

Minimum Viable Product for RTS game?

How to know if I am a 'Real Developer'

Players preemptively rolling, even though their rolls are useless or are checking the wrong skills

Did ancient Germans take pride in leaving the land untouched?

What can I do to encourage my players to use their consumables?

Can I use a single resistor for multiple LED with different +ve sources?

Is practicing on a digital piano harmful to an experienced piano player?

Is it possible to detect 100% of SQLi with a simple regex?

Process substitution inside a subshell to set a variable

If I tried and failed to start my own business, how do I apply for a job without job experience?

Why don't you get burned by the wood benches in a sauna?

Renting a 2CV in France

Promise.all returning empty objects

What could cause an entire planet of humans to become aphasic?

How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?

Size problems when plotting xy/(x^2+2y^2)

I am a giant among ants

Why is Shelob considered evil?



Explicit way to check whether a function was called from within the Window


Is there a better way to do optional function parameters in JavaScript?Trigger a button click with JavaScript on the Enter key in a text boxWhat is the best way to add options to a select from as a JS object with jQuery?(Built-in) way in JavaScript to check if a string is a valid numberHow to check whether a string contains a substring in JavaScript?What is the correct way to check for string equality in JavaScript?Is there a standard function to check for null, undefined, or blank variables in JavaScript?Check if a variable is of function typeCheck whether a string matches a regex in JSHow do I return the response from an asynchronous call?













6















Is there a more explicit way of checking whether a function was called from within Window than if (typeof this.value == "undefined")
in the code below?



So that it is apparent that I am checking against Window, something like: if this.name === "Window".






function get_caller() {
if (typeof this.value == "undefined") {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>












share|improve this question




















  • 1





    What happens when somebody calls get_caller.call({value: "I am not in window"})?

    – VLAZ
    2 hours ago
















6















Is there a more explicit way of checking whether a function was called from within Window than if (typeof this.value == "undefined")
in the code below?



So that it is apparent that I am checking against Window, something like: if this.name === "Window".






function get_caller() {
if (typeof this.value == "undefined") {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>












share|improve this question




















  • 1





    What happens when somebody calls get_caller.call({value: "I am not in window"})?

    – VLAZ
    2 hours ago














6












6








6








Is there a more explicit way of checking whether a function was called from within Window than if (typeof this.value == "undefined")
in the code below?



So that it is apparent that I am checking against Window, something like: if this.name === "Window".






function get_caller() {
if (typeof this.value == "undefined") {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>












share|improve this question
















Is there a more explicit way of checking whether a function was called from within Window than if (typeof this.value == "undefined")
in the code below?



So that it is apparent that I am checking against Window, something like: if this.name === "Window".






function get_caller() {
if (typeof this.value == "undefined") {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>








function get_caller() {
if (typeof this.value == "undefined") {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>





function get_caller() {
if (typeof this.value == "undefined") {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago







barciewicz

















asked 2 hours ago









barciewiczbarciewicz

665313




665313








  • 1





    What happens when somebody calls get_caller.call({value: "I am not in window"})?

    – VLAZ
    2 hours ago














  • 1





    What happens when somebody calls get_caller.call({value: "I am not in window"})?

    – VLAZ
    2 hours ago








1




1





What happens when somebody calls get_caller.call({value: "I am not in window"})?

– VLAZ
2 hours ago





What happens when somebody calls get_caller.call({value: "I am not in window"})?

– VLAZ
2 hours ago












2 Answers
2






active

oldest

votes


















6














Just check if this is window:






function get_caller() {
if (this === window) {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>








share|improve this answer








New contributor




Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





















  • Or undefined, in strict mode, which is the more common case today with webpack/modules.

    – Madara Uchiha
    58 mins ago



















0














You can check if this==window or if strict mode is on check if this is undefined,






function get_caller() {
"use strict"; // !this is used for strict mode check
if (this == window || !this) {
console.log('function get_caller called from window')
}
else {
console.log('function get_caller called by button press')
}
}

btn.addEventListener('click', get_caller)
get_caller()

<button id="btn">Get caller</button>








share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54850048%2fexplicit-way-to-check-whether-a-function-was-called-from-within-the-window%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    Just check if this is window:






    function get_caller() {
    if (this === window) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>








    share|improve this answer








    New contributor




    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















    • Or undefined, in strict mode, which is the more common case today with webpack/modules.

      – Madara Uchiha
      58 mins ago
















    6














    Just check if this is window:






    function get_caller() {
    if (this === window) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>








    share|improve this answer








    New contributor




    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















    • Or undefined, in strict mode, which is the more common case today with webpack/modules.

      – Madara Uchiha
      58 mins ago














    6












    6








    6







    Just check if this is window:






    function get_caller() {
    if (this === window) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>








    share|improve this answer








    New contributor




    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.










    Just check if this is window:






    function get_caller() {
    if (this === window) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>








    function get_caller() {
    if (this === window) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>





    function get_caller() {
    if (this === window) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>






    share|improve this answer








    New contributor




    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer






    New contributor




    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered 2 hours ago









    SnowSnow

    15611




    15611




    New contributor




    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    Snow is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.













    • Or undefined, in strict mode, which is the more common case today with webpack/modules.

      – Madara Uchiha
      58 mins ago



















    • Or undefined, in strict mode, which is the more common case today with webpack/modules.

      – Madara Uchiha
      58 mins ago

















    Or undefined, in strict mode, which is the more common case today with webpack/modules.

    – Madara Uchiha
    58 mins ago





    Or undefined, in strict mode, which is the more common case today with webpack/modules.

    – Madara Uchiha
    58 mins ago













    0














    You can check if this==window or if strict mode is on check if this is undefined,






    function get_caller() {
    "use strict"; // !this is used for strict mode check
    if (this == window || !this) {
    console.log('function get_caller called from window')
    }
    else {
    console.log('function get_caller called by button press')
    }
    }

    btn.addEventListener('click', get_caller)
    get_caller()

    <button id="btn">Get caller</button>








    share|improve this answer




























      0














      You can check if this==window or if strict mode is on check if this is undefined,






      function get_caller() {
      "use strict"; // !this is used for strict mode check
      if (this == window || !this) {
      console.log('function get_caller called from window')
      }
      else {
      console.log('function get_caller called by button press')
      }
      }

      btn.addEventListener('click', get_caller)
      get_caller()

      <button id="btn">Get caller</button>








      share|improve this answer


























        0












        0








        0







        You can check if this==window or if strict mode is on check if this is undefined,






        function get_caller() {
        "use strict"; // !this is used for strict mode check
        if (this == window || !this) {
        console.log('function get_caller called from window')
        }
        else {
        console.log('function get_caller called by button press')
        }
        }

        btn.addEventListener('click', get_caller)
        get_caller()

        <button id="btn">Get caller</button>








        share|improve this answer













        You can check if this==window or if strict mode is on check if this is undefined,






        function get_caller() {
        "use strict"; // !this is used for strict mode check
        if (this == window || !this) {
        console.log('function get_caller called from window')
        }
        else {
        console.log('function get_caller called by button press')
        }
        }

        btn.addEventListener('click', get_caller)
        get_caller()

        <button id="btn">Get caller</button>








        function get_caller() {
        "use strict"; // !this is used for strict mode check
        if (this == window || !this) {
        console.log('function get_caller called from window')
        }
        else {
        console.log('function get_caller called by button press')
        }
        }

        btn.addEventListener('click', get_caller)
        get_caller()

        <button id="btn">Get caller</button>





        function get_caller() {
        "use strict"; // !this is used for strict mode check
        if (this == window || !this) {
        console.log('function get_caller called from window')
        }
        else {
        console.log('function get_caller called by button press')
        }
        }

        btn.addEventListener('click', get_caller)
        get_caller()

        <button id="btn">Get caller</button>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 2 hours ago









        Code_ModeCode_Mode

        906714




        906714






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54850048%2fexplicit-way-to-check-whether-a-function-was-called-from-within-the-window%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Szabolcs (Ungheria) Altri progetti | Menu di navigazione48°10′14.56″N 21°29′33.14″E /...

            Discografia di Klaus Schulze Indice Album in studio | Album dal vivo | Singoli | Antologie | Colonne...

            How to make inet_server_addr() return localhost in spite of ::1/128RETURN NEXT in Postgres FunctionConnect to...