2

I am trying to get the current page URL and parameters but JavaScript and jQuery are returning the previous page's URL.

I am currently using:

$(document).ready(function() {

            function getUrlParameter(sParam){
                var sPageURL = window.location.search.substring(1);
                var sURLVariables = sPageURL.split('&');
                    for (var i = 0; i < sURLVariables.length; i++) {
                        var sParameterName = sURLVariables[i].split('=');
                        if (sParameterName[0] == sParam) {
                            return sParameterName[1];
                        }
                    }
            } 

            var charid = getUrlParameter('id');
            var charname = getUrlParameter('name');
            console.log(charid);
            console.log(charname);
    });

Sometimes if I Reload/Refresh the page it will give me the current/correct URL.

EDIT: I set the links on the previous page with this code

$.getJSON(ip + 'list_query.php', function(data) {
        // data now contains array
        for(var i = 0; i < data.length; i++)
        {
            var fl = data[i]['superhero_name'].slice(0,1);
            $("#" + fl).append("<a href='" + ip + "biography.html?id=" + data[i]['id'] + "&name=" + data[i]['superhero_name'] + "'>" + data[i]['superhero_name'] + "</a");
        }
        });
arcaderob
  • 481
  • 1
  • 7
  • 21
  • Can you be more specific on how to reproduce the problem? – Ludovic Feltz Nov 13 '14 at 00:13
  • @Ludovic have a hyperlink on the previous page the previous page that has it's href generated by a for loop and inserted as an attribute by jQuery. when I click the link i am taken to the page that has the code listed here but it gets the href from the previous page for some reason. – arcaderob Nov 13 '14 at 00:19
  • Ok i see, i'm trying to reproduce your problem with two page linked with each other, can you post some more code to show how you set your URL? – Ludovic Feltz Nov 13 '14 at 00:21
  • @Ludovic I added some code – arcaderob Nov 13 '14 at 00:23
  • I don't think the problem come from your previous page. What is on your current page before your JQuery code? – Ludovic Feltz Nov 13 '14 at 00:28
  • @Ludovic Also, the URL in the browser is correct but the output in the console is undefined. If I output the URL itself it comes out as the URL from the previous page – arcaderob Nov 13 '14 at 00:28
  • don't you override the URL previously on your code? something like `window.location.href="previousPage.html"` – Ludovic Feltz Nov 13 '14 at 00:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/64827/discussion-between-ludovic-and-arcaderob). – Ludovic Feltz Nov 13 '14 at 00:31
  • cannot comment (not enough reputation), but this may help: [Finding previous URL from window.history][1] [1]: http://stackoverflow.com/questions/17030074/finding-previous-url-from-window-history –  Nov 13 '14 at 00:56
  • @CharlSteynberg That is somewhat the opposite of my problem. say the first page is yahoo.com and the second page is google.com, when i check the url on the second page it is returning the first url for some reason – arcaderob Nov 13 '14 at 00:59
  • Since it will work if you refresh, it sounds to me like a caching issue. Have you tried adding the meta tags to force the browser not to cache the page, or does it consistently work in a different browser? – Tim Nov 13 '14 at 20:36

2 Answers2

1

are you looking for the current page URL, or the previous page URL, or, are there conditions involved?

For the current page URL, try:

console.log(window.location.href);

Unfortunately you cannot sniff the visitor's history, but if you want to track their history while browsing your domain, it is very easy to just set their current page aka: "window.location.href" in LocalStorage, and name the keys accordingly and hence create a list/index -- or even save it in cookie(s). The latter you can then use in PHP directly; whereas the former (LocalStorage) you can send to the server via XMLHTTpRequest.

If you are having this issue due to a javascript framework or plug-in, try to disable them temporarily and test to see if you get the same results in order to trace the root of the problem.

Update

Regarding Javascript frameworks, the following thread on GitHub may be useful with: window.location issues

0

It's very simple to get the current page's URL with vanilla JS. Do it like this:

alert(document.location.href);

Hope this helps!

Ian Hazzard
  • 7,661
  • 7
  • 34
  • 60