javascript location href changed queryString (&gt to >) - javascript

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;

Related

encodeURIComponent does not return encoded URI

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)

how to get page url without decode in javascript?

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;

Remove jQuery AJAX timestamp from query string

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

Pass text parameter to the url variable at .load function on jQuery

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

javascript / jquery get URL without hash

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

Categories