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");
}
});