0

I am trying to write some JavaScript where I can pass the path of the current page URL to another function ; specifically would like to add it as a a value in a custom variable for Google Analytics. However, since I don't know JavaScript, I thought it would be best to see if I can pass the variable to a simple alert. Alas, I can not. I have checked out several posts on this site and can't get this to work. Some of the threads I've checked are:

need to pass variable to function
How to pass this variable to this function?
Pass variable to external javascript?

I've also tried Google and some JavaScript tutorials, and playing around with single, double quotes, brackets, etc...nothing works. Can anyone help please? Thank you.

            <script type="text/javascript">
            $(document).ready(function() {
                var pdpURL = window.location.pathname;
            });
            function popup() {
                alert('pdpURL');
            }
            </script>

The pop up appears but it just says pdpURL in it rather than the URL.

6 Answers 6

4

Several problems:

  • you're not calling popup

  • pdpURL is local to the handler, but popup is global

  • the alert is given a string, not a variable reference


$(document).ready(function() {
    var pdpURL = window.location.pathname;

    function popup() {
        alert(pdpURL);
    }

    popup();
});
Sign up to request clarification or add additional context in comments.

Comments

2

First of all, you don't need quotes around your variable name when you call it. Secondly, that variable is scoped to the $(document).ready() function, and as such will not be accessible to your other function. The variable must be global for you to be able to do this, which you can do by declaring the variable outside of your functions.

1 Comment

Thank you, this solved it! I really appreciate your explanation, as it helps me learn more about JavaScript syntax. Now I can sleep tonight :)
1

Try this:

 <script type="text/javascript">
            function popup(path) {
                alert(path);
            }
            $(document).ready(function() {
                var pdpURL = window.location.pathname;
                popup(pdpURL);
            });
            </script>

Comments

0

You need to replace the single quotes. Try alert(pdpURL)

Comments

0

Of course it isn't going to work.

If you imagine functions, and their variables, like boxes, then you have a variable inside one box that can't seen by the other

$(document).ready(function() {
  var pdpURL = window.location.pathname;
});
function popup() {
  alert('pdpURL');
}

You have two options.

  • move popURL to a global level
  • move popup inside your anonymous function

Example 1

var pdpURL;
+------------------------------------------
|  // This is box 1, an anonymous function|
|  // can see pdpURL                      |
+------------------------------------------

+------------------------------------------
|  // This is box 2 called popup          |
|  // Can't see anything inside box 1     |
|  // But can also see pdpURL             |
+------------------------------------------

Example 2

+------------------------------------------
|  // This is box 1, an anonymous function|
|  var pdpURL                             |
|  +----------------------------------+   |
|  |  // This is box 2 called popup   |   |
|  |  // Can see inside box 1         |   |
|  +----------------------------------+   |      
|                                         |
+------------------------------------------

Comments

0

You need to consider that the $(document).ready(); will only fire when the whole page has finished loading.

That means that the code after this may actually excute before the page finishes loading.

You should always consider the "event" order when looking at order of execution.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.