Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?!

like image 887
saleh mosleh Avatar asked Mar 13 '26 16:03

saleh mosleh


2 Answers

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.

like image 76
KOUSIK MANDAL Avatar answered Mar 15 '26 04:03

KOUSIK MANDAL


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.

like image 29
rafaelkendy Avatar answered Mar 15 '26 04:03

rafaelkendy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!