codepuppy
codepuppy

Reputation: 1140

How do I detect what invoked a jquery script

Apologies for no code. But I don't have a starting point for this question.

I have searched jquery and javascript documentation but I must be using the wrong terms because I am not getting anywhere.

I wish to take action within a jquery script dependant on what invoked the script.

i.e. I have the same jquery script in several php files. I don't want to create a separate script for each php file. I want to know which php file invoked that script and alter it's behaviour accordingly.

It seems like a perfectly reasonable thing to want to do so I am hoping that it is a really easy question.

Upvotes: 0

Views: 76

Answers (2)

Bergi
Bergi

Reputation: 664548

Let the PHP file echo a config object to tell the JS how to behave (e.g. on what type of page it executes):

<script type="text/javascript">
    config = <?php
        $config = array("page" => …, "state" => …);
        echo json_encode($config);
    ?>;
</script>
<script type="text/javascript" src="/common.js"></script>

Upvotes: 0

Alnitak
Alnitak

Reputation: 339816

Look in the window.location object - it'll tell you the URL of the containing HTML page.

Upvotes: 2

Related Questions