getting ajax request url on the recieving page - javascript

i have the following setup:
an aspx page called content.aspx
from it i make an ajax request to population.aspx with all the parameters i need and embed the response which is html data of population.aspx
in population.aspx i have a function that needs to use the parameters and a function that takes them out of the url:
function getParameterByName(name) {
var url = window.location.href.toLowerCase();
name = name.toLowerCase().replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(url);
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
when i use it i get the url of content.aspx which does not contain the required parameters.
is there a way to get the request url i used and not window.location?
thanks.

Related

Pass URL parameters through to next page with Javascript (or not)

I think this is simple but can't seem to find the answer.
In our autoresponder, when a contact clicks a link to visit a page, the contact id and email are passed through to the next page.
So by clicking an email, the contact lands on page.com/1/?id=123&email=name#gmail.com
On that page, and there is a button to click to go to the next page...
what do I need to do so the parameters pass to the next page and the contact lands on page.com/2/?id=123&email=name#gmail.com?
Thanks for the help!
If the page number is all that changes:
button.addEventListener('click', () => {
const currentPage = Number(window.location.href.match(/\d+/));
window.location.href = window.location.href.replace(/\d+/, currentPage + 1);
});
Edit: here is a function to get the querystring parameters
from This link
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
If you already know the base url, and have the parameters just combine the two and redirect.
document.getElementById("myButton").onclick = function () {
var url = "page.com/2"; //get your base url how ever you're doing it.
var queryString = "?id=123&email=name#gmail.com";
var fullUrl = url + queryString;
window.location.href = fullUrl ;
};

How to store text box value to be passed to getJson request as query parameter

I need to be able to store the text value of a search box term, which can be used as a query parameter in a getJson request. I'm appending the search term to the end of the url the user is taken to after hitting the enter key, but the issue is that on the location replacement, it shows up as an error because the url for the page is /faq/search-results/.
$(".faq-search").keyup(function(e){
if(e.which == 13) {
window.location.replace("/faq/search-results/" + $(".faq-search").text());
}
});
Once the user has been sent to the search results page, I have a script which, if the user is on that url, is supposed to grab the search term from the pathname in the url, and submit it as a query parameter to the getJson request:
if(window.location.pathname == "/faq/search-results/"){
$("document").ready(function(e) {
var url = window.location.pathname;
var exp = url.split(/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/);
var question = exp[5].split("/")[3];
$.getJSON("//search.url-to-search.com?q=" + question + "&results_type=faq", {
},
//Get results and make 'em look good
function(data) {
console.log(data);
$.each(data.data, function(i, data) {
if(data.type === "FAQ"){
$(".faq-results").append("<li class='result-item'><h3>" + data.title + "</h3><p class=\"content\">" + data.text + "</p><p class=\"category\">" + data.type + "</p></li>");
}
});
});
});
}
I believe the issue is that it won't fire off the request because its looking for only /faq/search-results/. I think I need a way to store the search term as a variable and pass it as a query parameter, but not sure how to accomplish this, as I believe it would make the variable out of scope.
A couple of things are wrong in your code:
first to collect the input value use .val() note text().
Secondly you are not passing the input value as a query string you are adding it to the url path /helloWorld. I think it is better to add as a query string ?q=helloworld.
I have therefore adjusted your code, removed your code to extract the text from the path and implemented a function to extract a named query param, this function is called getParameterByName.
The code below should be pretty much self explanatory.
$("document").ready(function(e) {
//
// Collects the input param as passes it as a query string note
// ?q= our string
//
$(".faq-search").keyup(function(e) {
if (e.which == 13) {
window.location.assign("file:///C:/Users/spydre/Desktop/text.html?q=" + $(".faq-search").val());
}
});
// snippet gets a query param from url
function getParameterByName(name, url) {
if (!url) {
url = window.location.href;
}
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
// collect any query string param whos name is q
var question = getParameterByName("q");
if (question) {
// pass question to our getJson
$.getJSON("//search.url-to-search.com?q=" + question + "&results_type=faq", {},
//Get results and make 'em look good
function(data) {
console.log(data);
$.each(data.data, function(i, data) {
if (data.type === "FAQ") {
$(".faq-results").append("<li class='result-item'><h3>" + data.title + "</h3><p class=\"content\">" + data.text + "</p><p class=\"category\">" + data.type + "</p></li>");
}
});
});
} //if question
})

GET parameters from URL

I have an URL like http://www.test.pl/product-pol-4406.html
With Geasemonkey scripts I want to get the "4406" part from the URL, but I don`t have any idea how to do it. My code is:
var input=document.createElement("input");
input.type="button";
input.value="Edytuj";
input.alt="visitPage";
input.onclick = visitPage;
input.setAttribute("style", "font- size:18px;position:absolute;top:120px;right:40px;");
document.body.appendChild(input);
function visitPage(){
window.location='https://test.pl/panel/product-edit.php?idt=4406';
}
Any suggestions? Please.
use below function to get your 'idt' value in javascript
function getParameterByName(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
var result= = getParameterByName('idt');
Just use
var url = window.location.href;
To get the full URL. According to your needs you could parse the URL with regex afterwards to get what you need, or e.g. split it to get parts of it with a seperator that makes sense with the type of URLs you are working with.
If you want to take the product id from the given url and then pass to the php page. You can try this
var url = "http://www.test.pl/product-pol-4406.html" //window.location.href
if(url){
url = url.substr(url.lastIndexOf("/") + 1);
var productID = url.match(/\d+/);
alert(productID);
}
Update:
function getProductID(){
var url = "http://www.test.pl/product-pol-4406.html" //window.location.href
if(url){
url = url.substr(url.lastIndexOf("/") + 1);
return url.match(/\d+/);
}
}
And then call the function inside the visitePage() function
function visitPage(){
var productID = getProductID();
if(productID){
window.location='https://test.pl/panel/product-edit.php?idt='+productID;
}
}

getParameter - URL parameter with dynamic name values

I've been strugling looking for a way to set a cookie if a URL parameter is persent. The problem is, the name of the URL parameter is half dynamic.
The URL would be:
http://zzzz.test.bbbb/index.html?transaction[XXXX][zzzzz]=YYYYY
Where XXXX and zzzzz are part o the URL name but can change according to what's in the link.
How would the correct getParameterByName function look like in order to recognize the URL parameter transaction[XXXX][zzzzz] ?
I've tried this but it does not work:
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp(name + '(?:\\[\\d+\\]?:\\[\\d+\\])?=' + '(.+?)(&|$)'),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
if (getParameterByName("transaction")) {
set_cookie ("Transaction_ID", getParameterByName("transaction"));
}
Any ideas?
Check this JSBIN. This will help you.
Replace locatStr by window.location.search; The below code will work in all scenarios
function getParameterByName(){
var locatStr = '?xyz=123&transaction[XXXX][zzzzz]=YYYYY',
searchStr = locatStr.split('?')[1],
matchArr = searchStr.match(/transaction\[[a-zA-Z0-9]+\]\[[a-zA-Z0-9]+\]/gi),
para;
if(matchArr){
var temp = searchStr.split(matchArr[0]+'=')[1];
return ((temp.indexOf('&')!=-1) ? temp.split('&')[0] : temp);
}
else{
return false;
}
}
var param = getParameterByName();
console.log(param);
if(param){
console.log('set cookie here');
}
else{
console.log('no cookie present');
}
P.S. Dont forget to accept the answer if satisfied

Show/hide div based on URL - works locally but not on live server

I'm using some jQuery to show/hide a div based on a URL parameter (?rp=):
$(function () {
if (document.location.href.indexOf('rp') > -1) {
$('#rphide').hide();
} else {
$('#rphide').show();
}
});
It works locally and on a development server, however on the live server the div is permanently hidden EVEN if there's no ?rp= in the querystring. I'm confused as to why this is happening? Thanks.
it is hard to find issue with such details but here is another approach to do the same.
you are trying to search string "rp" in url, which is not good in case if you have rp in domain name. so better approach would be find query in url.
Use this function to get param.
$(function () {
if (getQueryStringParamByName('rp') != "") {
$('#rphide').hide();
} else {
$('#rphide').show();
}
});
function getQueryStringParamByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

Categories

Resources