I would like to create a bookmarklet that appends the url of the current page to another predefined url.
javascript:(function(){location.href='example.com/u='+encodeURIComponent(location.href)}());
However, the returned url is not encoded, but when I tried encodeURIComponent(encodeURIComponent(location.href)) it worked. I don't understand why doubling encodeURIComponent makes the different?
Try this:
var href = (a) => {return 'example.com/u='+encodeURIComponent(a)}
and call href to get encoded url.
var encoded_url = href(location.href)
Related
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;
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! :)
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 am trying to pass a text by parameter (with whitespaces) to the load function and it seems it doesn't work.
Currently i am doing this:
var text ="hello world this is an example";
$("#selector").load("http://"+ document.domain + "/myfunction/"+text);
Is there any way to do it?
If i call the function by URL directly, not with jQuery, it works well.
Thanks.
You should encode "text" with encodeURI:
var text = encodeURI('hello world this is an example');
This will ensure that your whitespaces are replaced with url compatible characters, which your browser does internally when you're directly accessing the url.
Calling the function by URL directly might works well in your browser. But it is not future proof. You must encode your url with encodeURI function. After appending user given data.
var text ="hello world this is an example",
url = encodeURI("http://"+ document.domain + "/myfunction/"+ text);
$("#selector").load(url);
And on server side you can do something like this to get back user entered data.
$data = urldecode($_SERVER['REQUEST_URI']);
// This will return
// /myfunction/hello world this is an example