I am trying to use JavaScript for redirecting to affiliate links.
The problem is that the url contains & which changes to & after the redirect.
location.href ="http://affiliatelink.com/?a=xxxxx&e=xxxx"
will in the web browser be changed to
http://affiliatelink.com/?a=xxxxx&e=xxxx
With & in the url the affiliate link doesn't work.
How can I fix this problem? encodeURIComponent()?
Yes, you need to %encode the URL with the encodeURIComponent function...
encodeURIComponent('&') >> "%26"
Do this by popping the URL in the brackets of the encodeURIComponent function...
var myURL = 'http://address.web/?info=data&info=data';
var encodeURL = encodeURIComponent( myURL );
... or you could use the traditional string .replace() method...
var reg = new RegExp( '&', g );
var myURL = 'http://address.web/?info=data&info=data';
var encodeURL = myURL.replace( reg, '%26' );
The first method has the advantage of catching all %encode-able characters, where the second method only deals with &s.
This is because you transfer your url from code behind, perhaps you can try to deal with the url in the front-end like hardcode in javascript, then your problem will be solved.
In my case, I gather my url from xml(web.config) and xml should be used & , so I change the way for placing parameter(url) in the xml, in stead of placing the parameter(url) in the javascript.
For your reference! :)
Related
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 can I get page URL from window.location.href without decode in javascript?
For example we can not get exactly this URL: http://example.com/report#title=example_report&url=project_chart%2F%3Fproject_id%3D77.
When we use window.location.href in javascript , we will get this URL:
http://example.com/report#title=example_report&url=project_chart/?project_id=77.
But I want to get exactly the same real URL.
Any solution?
Edited
as #Eugenio told, $(document)[0].URL works fine , but Is it safe?!
Try to use encodeURI.
As for example;
var url = window.location.href;
var originalUrl = encodeURI(url);
This function(encodeURI) encodes special characters,
except: , / ? : # & = + $ #
You can use encodeURIComponent() to encode these characters.
You can use encodeURIComponent, but you have to get the part of a string you want to encode.
encodeURIComponent(window.location.href.split('&url=')[1])
Or you can use RegExp to be more precise.
Just to make a clear and concise answer I will sum up all the comments.
For your problem the best solution is to use document[x].url where x is the index of the URL part that you want to use.
The main difference for your problem between window.location.href and document.url is that the last one gives you the URL in a string format, whilest the other return the URL already parsed.
Using either one is completely normal and safe and is widely adopted in all modern browsers.
var url1 = document.URL;
var url2 = window.location.href;
document.getElementById("documentUrl").append (url1);
document.getElementById("windowLocationUrl").append (url2);
<div id="documentUrl">document.url: </div>
<div id="windowLocationUrl">window.location.href: </div>
There is no difference in this particular snippet example because there are no parameters attached to the URL. Anyway, hope this helped. Cheers!
as #Eugenio told,
i use below code and it works fine:
var url = $(document)[0].URL;
User inputs a web address that I want to get only the tail from, as I do know what site he inputs.
So first I want to remove the "main" URL and get what ever is at the end, so my action is:
Original link: http://example.com/something
var n=e.split("http://example.com/");e=n[1];
And I will get "something"
The problem is that site can also be secured, thus having https not http. Therefore the split wont work.
How do I define a split function, that would work like this:
split("http://example.com/ || https://example.com/")
I do not want to split by looking at "//" or anything of that sort, I want an exact address.
If you like it clear and want to avoid regular expressions, try this:
var n=e.split("http://example.com/",2).pop().split("https://example.com/",2).pop();
If you wish to know the host you can do so by using this code instead in JavaScript:
window.location.host
Source Get The Current Domain Name With Javascript (Not the path, etc.)
You can also use window.location.path to get the URL that was requested, combining those you get:
window.location.host + window.location.pathname
For me, this outputs stackoverflow.com/posts/25203020/edit while writing this reply.
var s = "http://example.com/something";
function split (url) {
var r = /([^:]+):\/\/([^\/]+)\/(.*)/gi;
var a = r.exec(url)
return [a[1], a[2], a[3]];
}
I have the following code in my page.
<script>
var url = "http://localhost/login.aspx?returnUrl=/ABC/abc.aspx>_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 >_no changed to >_no
Is there any method to keep >_no remain unchange after redirect?
It is not allow to use other parameter name insteand of >_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 :)
> 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>_no=1234567&code=SC";
window.location.href = url;
I know I can read the hash value of a URL with javascript/jquery. But is is possible that I can read the trailing bit? Finding the last piece of the URL
I have a domain. http://www.blah.com/
each section of the domain resides under a URL that is slug like "this-page" example
http://www.blah.com/service/
(with or without the trailing slash) But I want to know if I can find "service" in the URL with JavaScript, without Server Side intervention. I know I could do it if I had
http://www.blah.com/#service
I don't know Im just curious, I really don't know what I would look for otherwise so this is my first stop in my search cause I am clueless..
var p = location.pathname;
p = p.substring(p.length-1) == '/' ? p.substring(0, p.length-1) : p;
p.split('/').pop();
Use regex:
last_bit = $(location).attr('href').replace(/https?:\/\/[^\/]+/i, "");