Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open an URL followed by a hash sign using location.href or similar

There is a page with a "share at whatsapp" button. This creates and executes an URL like:

whatsapp://send?text=Some text followed by a link - http://link_to_this_page#something

The problem is that the browser (I've tested only with Chrome for now) automatically deletes from the hash sign to onwards.

I have tried the basic:

var href = 'whatsapp://send?text=Example text - ';
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.';
location.href = href + uri;

I tried too with location.replace(), location.assign() and window.open() with no luck.

So the question is, how can I do? It's imperative to use the hash because it tells to the target page that it has to do some things in javascript (which could take more time to change).

like image 328
kosmos Avatar asked Jan 28 '26 10:01

kosmos


1 Answers

You should be encoding anything that is in the query string.

location.href = href + encodeURIComponent(uri);

You probably should be doing:

var href = 'whatsapp://send?text=';
var text = 'Example text - ';
var uri = location.protocol + '//' + location.host + location.pathname + '#gm.';
location.href = href + encodeURIComponent(text + uri);
like image 142
epascarello Avatar answered Jan 31 '26 00:01

epascarello



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!