Remove jQuery AJAX timestamp from query string - javascript

How can I remove the jQuery AJAX cache preventer (_=3452345235) when dealing with string URLs?
I am writing a global AJAX fail handler and to do this I need to know which URL failed, but everytime I check the URL of the request which failed the jQuery cache query string means all my URLs are different so I need to remove this from the string before doing any more work
So if my URL is (as a string, not window.location) is
/device/page/?page=2&_=23523452345
I want to solely remove the timestamp to be left with
/device/page/?page=2

THis should work for you.
var url = '/device/page/?page=2&_=23523452345';
url = url.replace(/&?_=[0-9]*/, '');
Regards.
UPDATE
The first work for at the end and in the middle of the query string but not at the beginning of the query sting ?_=212115211&page=2.
This regex works for any location of the _ param.
url = url.replace(/&?_=[0-9]*/, '');
Thanks

Related

How to set Metatitle for a user-entered javascript hashed URL via PHP

I want to set a specific metatitle to a javascript hashed url. I want to set it via PHP so that when google crawls it the metatitle is already there.
An example of a page: https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25
In this url /en/shop/ is the wordpress page, and #!/men+long+sleeve+shirts?q=P25 is set by javascript.
I have this code in the functions.php to try to set the title:
if (is_page(194) and (strpos($url, 'men+long+sleeve+shirts') !== false))
{$new_title = "long sleevetitle";};
return $new_title;
But how do I get 'men+long+sleeve+shirts' or '?q=P25' in the $url variable?
Is there a way to get the user-entered url in PHP?
You can use combination of strpos() and substr();
Like this:
$string = "https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25";
var_dump(substr($string,strpos($string,'?'))); // output '?q=P25'
It will cut whole string and leave only the part from ? to end.
Explode method:
Or you can use alternate, explode method like this, which will ensure that it will target only last element with question mark.
$string= "https://weslaydragons.com/en/shop/#!/men+long+sleeve+shirts?q=P25";
$string2= explode('?',$string);
var_dump(end($string2)); // output 'q=P25'
Edit:Both methods would work with "#" also, just replace '?' with '#'.
Refs:
http://php.net/manual/bg/function.strpos.php
http://php.net/manual/bg/function.substr.php
http://php.net/manual/bg/function.end.php
http://php.net/manual/bg/function.explode.php

How to correctly read encoded get varible

I have a search engine that does the following things:
Read an input value and encode it using js, then redirect.
//read and save into `query` var
window.location.href = "/search/" + encodeURIComponent(query);
So if user enters
What is the meaning of & sign ?
The ulrl can't end up like this;
expample.com/search/What%20is%the%meaning%20of%20&this%20sign?
And instead get:
expample.com/search/What%20is%the%meaning%20of%20&26this%20sign%3F
Now when I dump the $_GET['parameters'] i get
string() "search/What is the meaning of "
I expect to get:
What is the meaning of & sign ?
I have tried:
$val = urldecode($_GET['parameters']);
But I have had no luck, Maybe I should change the way javascript encodes the url, what are your suggestions?
PHP decodes URL paramaters automatically into the $_GET superglobal as long as you're using the standard query string syntax. If you use your own syntax, you have to roll your own code (you already have custom code in the input form).
The raw URL can be fetched from $_SERVER['REQUEST_URI'] and parsed with the text manipulation tool of your choice. It's worth noting that this isn't an uncommon set up (many PHP frameworks do things this way).
You've mentioned that you're calling the following to obtain the value of the user's query:
$val = urldecode($_GET['parameters']);
This implies that a URL calling your PHP page would have a shape similar to the following:
http://foo.bar/?parameters=<the query here>
The important thing to include in the URL is ?; when a URL is parsed, the ? signals that whatever comes afterward is a URL-encoded query.
Thus, in your javascript:
window.location.href = "/search/?parameters=" + encodeURIComponent(query);
Then your existing code should work.
Just do
on client-side
window.location.href = "/search/" + query;
and on server-side
$val = urldecode($_GET['parameters']);

How to get last part of URL, ignoring any GET parameters?

I'm expecting URLs in this for:
/user/username
but end users can also add whatever get parameters they want, like so:
/user/username?foo=bar
With that said, using AngularJS, what's the preferred way for me to get just the username (which appears after /user/) without anything else after it?
You should use the $location service and its .path() method, then use a regular split() and indexing.
I doubt there's a dedicated function for it but it seems easy enough to pull it out of the string with the query.
Get the path with $location.path() and then both of these does the job for you.
url.substring(6, (url.indexOf('?') != -1 ? url.indexOf('?') : url.length))
url.split('/')[2].split('?')[0]
Same question here: Is there a built-in way to get the current URL without any query parameters?
you can use window.location or $location service to get the path and then using split function
like this
var url = "/user/username?foo=bar";
var check = url.split('/user/');
var username = check[1].split('?');
console.log(username[0]);
you can find username by applying multiple split on your url. hope this is what you want.

Extracting from URL - Long URL

I'm currently trying to extract from ReturnUrl= ... I want to extract the URL from the link below using javascript. Can anyone help?
http://testdealbuilderCCMS/questionnaire.aspx?db_template_reference=Construction: Westfield Services Agreement&ContractDescription=Facilities Contract&NatureServices=FACILITIES&SiteDescription=Retail Units&ThirdPartyAgreementsList=&ServiceFee=1000&ReturnUrl=http://localhost:4965&launcher.aspx?directLink=PX&caseKey=7ccef65756504a79bc3a4a6687c0d9555e519ec9079241c9944c6a523704&PXid=
there are lots of edge cases here that will make this fail. So be careful, use this only if your string always ends with the ReturnURL parameter.
Find the position of the ReturnURL= in the string then get the substring from ReturnURL= position + ReturnURL= length, to the end.
http://jsfiddle.net/3hvajedg/1/
the_string = 'http://testdealbuilderCCMS/questionnaire.aspx?db_template_reference=Construction: Westfield Services Agreement&ContractDescription=Facilities Contract&NatureServices=FACILITIES&SiteDescription=Retail Units&ThirdPartyAgreementsList=&ServiceFee=1000&ReturnUrl=http://localhost:4965&launcher.aspx?directLink=PX&caseKey=7ccef65756504a79bc3a4a6687c0d9555e519ec9079241c9944c6a523704&PXid=';
alert(the_string.substring((the_string.indexOf('ReturnUrl=')+'ReturnUrl='.length)));

javascript location href changed queryString (&gt to >)

I have the following code in my page.
<script>
var url = "http://localhost/login.aspx?returnUrl=/ABC/abc.aspx&gt_no=1234567&code=SC";
window.location.href = url;
</script>
when i load the page, it redirect to
http://localhost/login.aspx?returnUrl=/ABC/abc.aspx>_no=1234567&code=SC
the parameter &gt_no changed to >_no
Is there any method to keep &gt_no remain unchange after redirect?
It is not allow to use other parameter name insteand of &gt_no in my project.
The problem not just happen in localhost.
Thanks!
You have arrived at a situation where you have generated an HTML encoded value value even though you didn't mean to :)
&gt is the HTML encoded value for the greater than character - >. You could try make sure that your gt_no parameter is the first parameter. This way, it will not be next to the ampersand (&) character and won't be interpreted as a HTML encoded value.
You could try URL Encoding the ampersand that is causing the issue:
var url = "http://localhost/login.aspx?returnUrl=/%26gt_no=1234567&code=SC";
var url = "http://www.google.com/login.aspx?
returnUrl=/ABC/abc.aspx&gt_no=1234567&code=SC";
window.location.href = url;

Categories