📜 ⬆️ ⬇️

JavaScript Requests

There is a need: pass parameters directly to JavaScript.
We can pass parameters through GET, POST, but when passing these parameters, a new page will simply load, i.e. if the browser cache is

some.so/index.php?id=2

and we request
')
some.so/index.php?id=3

then the server will generate a new page for us, it will not take some.so/index.php?id=2 from the cache (and it will be correct to do it).

The same situation is with POST, so it’s possible to say that GET and POST methods can pass parameters to javascript, but this is not entirely true. Because the parameters are transmitted to the server, new content is generated there, and there you can do anything. Although GET variables are taken directly from window.location.href, at least in some way server language re-generate JavaScript every time. and this is all just an imitation of JavaScript requests.

However, there is one character in the standard URL that will help us.



This is a pound symbol (#), although some say it is a symbol of the English currency, but this is not important now.

The trick is that if there is a cache

some.so/index.php#id:2

and we will request

some.so/index.php # id

then the second page will be taken from the cache (which it got after the first request).

So, let's get down to business: after the lattice character in the URL, it is allowed to use symbols
: _ -.
and any Latin characters.

Let the pair (key: value) be separated by a dot; the key itself is separated from the value by a colon, and the screening will be done using an underscore.

Thus, if we want to pass 2 parameters, it will look something like this:

some.so/index.php#id:2.par2:dgsdgd

if we want to transfer dot characters, colons or non-Latin characters, we do it like this:

some.so/index.php#id:_56_34.par2:_21_20_54

From the usual links some.so/index.php#chapter7 such requests to JavaScript (jR = JavaScript Request) can be distinguished by the presence of a colon (:) after #, since I have never seen someone put a colon there.

Writing js functions that will handle jR is not difficult - take window.location.href, trim it with regexp, split the string into an array, split the array elements into a key and value. That's all.

It really works, and really saves traffic, gives funny effects with AJAX, in general, experiment.

Of course, TBL would not be very happy for such use, but still, I think, client-side JavaScript deserved that now it will be able to receive parameters directly for itself. Hooray!

Source: https://habr.com/ru/post/15089/


All Articles