How to get parameter from hash url? - javascript

On some of my pages, I have a hash in the url like this: http://localhost/#/products/6959
How can I check if I have a product ID available and so I can save it in a variable to use later? Any help would be greatly appreciated!

You want a regexp to extract the number at the end of the url in javascript?
"http://localhost/#/products/6959".match(/[0-9]+$/);
Edit: Or if you want to make sure this is products:
"http://localhost/#/products/6959".match(/products\/([0-9]+$)/)[1]);
The parenthesis indicate a matching group that you find in [1].

You should be able to get that id out of the URL through accessing the params variable. In this case, params[:id] should do the trick.
Also, I do not recommend using a regex.

Related

How to substring variable and concatenate it with url in Thymeleaf

I want to substring a variable which is my JPA class id field and then add it to an URL in Thymeleaf.
My URL is like
/Myapplication/sortddoc/value=__${entity.id}__
So I need to do something like
${entity.id}.substr(0, 8)
before concatenating it to the URL.
I tried to create a new Transient field in my entity class containing the substring variable but it doesn't work because it seems like to need database field that I cant provide.
Can Anyone help me, please ?
You should be doing this with Thymeleaf's standard URL syntax, instead of concatenating string variables or using preprocessing (there is no need for either of those).
<a th:with="${value=#strings.substring(entity.id,0,8)}"
th:href="#{/Myapplication/sortddoc/(value=${value})}"></a>
or
<a th:href="#{/Myapplication/sortddoc/(value=${#strings.substring(entity.id,0,8)})}"></a>

What is the difference between 'path' and 'query' in a API call

I m trying to implement faceit API in my website.There are two end points from which I can retieve a player's information
1.https://open.faceit.com/data/v4/players?nickname=DeADLY2501&game=CSGO&game_player_id=76561198806878477
This is the first way.Below i will link a image.From faceit documentation(https://developers.faceit.com/docs/tools/data-api)
Here there is a parameter called 'game_player_id' which says it must be a string and 'query'.
Here is the second endpoint
Here in which a parameter is need called 'player_id' which is required needs to be a string and must be a 'path'.
Can some one please tell me what is the difference.Because in the first end point.We need 'nickname','game' and 'game_player_id'.
I just want to retrive a players information just from the id,So that can be made possible by the second endpoint.The problem is that.With the same 'player_id',I send calls for both end points.The first one sends a response successfuly.While the second endpoint says 'Not Found'.I gather that it might be that the type of parameter im making the request with are not proper for the 2nd endpoint.
Any help regarding this is appriciated thank you.
There is a formal definition here
But sometimes it's easier to see an example, consider https://stackoverflow.com/questions/62362966/?arg=3;other=word This is composed of several parts:
https - this is the scheme
stackoverflow.com - this is the hostname
questions/62362966/ - this is the path
arg=3;other=word - this is the query
Note that where your documentation above refers to a value appearing in a path or query, that does not mean it exclusively constitutes the path or query - there is also data/meta-data framing it.
Path and query - different parts of the URL. Basically, everything that goes after ? is query, everything between domain and ? is path.
See details here
In example below I mean the user_id is a path.
https://www.example.com/api/v2/users/{user_id}
https://www.example.com/api/v2/users/11
In example below I mean the limit and the page is a query.
https://www.example.com/api/v2/users?limit={limit_value}&page={limit_value}
https://www.example.com/api/v2/users?limit=50&page=1
A query goes after ? AND is made up of a key and a value in the form key=value. Each pair is separeted by a &.
A path is what goes after the first slash (/) and before ?. It doesn't require the key-value pattern. You just put the value in the right spot (i.e. between other slashes).

How to get the id of a post from a URL

I have a URL that is formatted like this:
http://localhost:3000/post/5dc07270e7179a5293d14e70
The part after /post/ contains an id as you see. I managed to obtain it by doing the following:
const path = this.props.location.pathname;
const postId = path.slice(6, 30);
Is there a better way of obtaining that id in React?
You can split on "/" and slice the result with a negative index (if the id is always the last part of the path).
console.log("http://localhost:3000/post/5dc07270e7179a5293d14e70".split("/").slice(-1)[0]);
Of course, you can preprocess that string and get id out from there, but for the brighter future you should do it another way.
An example of better use is react-router-dom since it lets you define parameters in your URL, and you can easily get that.
You can use withRouter to get the history and location details in the props. From there you can split the url by '/' and the length - 1 will be your id.
Or you can go raw javascript
let url = window.location.href.split('/')
console.log(url[url.length - 1])
If the id is not specified in the url like this
?id=yourid
Then the id must have to be at the end of the url for this to work.

url in javascript

This is a javascript geo-target code:
<script src='http://promos.fling.com/geo/txt/location.php?testip='></script>
Is possible add the results of it in the end of URL?
Fairfield, CT
The javascript result put after ?q=
Thanks!
You can assign a value to location.search. Anything you assign gets placed in front of the ?. You may need additional parsing to extract or append specific values and reconstruct it to form a query string. Note that the page will refresh when doing so.

Simple hashing function that is HTML id-friendly and case sensitive

I get some strings from an external source, and I display them in spans on my page.
I need a way to get back to those strings using document.getElementById() or jQuery's $("#XXXX"), so along with each string I get some sort of an identifier, I use that identifier as the ID of the span.
The problem is that the identifier I get could contain chars like + for example. Which is not allowed as a value for the id attribute http://www.w3schools.com/tags/att_standard_id.asp
Additionally, these identifiers are case-sensitive. So I thought of using a hashing function like SHA or MD5, to hash the identifiers I get, then use them as ids for my spans, and I can apply the hashing function again to find my element.
This seems complicated for such a simple functionality. Is there a better way to do this? or maybe a very simple hashing function that would guarantee id-friendly chars and case-sensitivity? (HTML's id is not case-sensitive, that's another reason to consider hashing functions)
Can you ditch the identifier you get and just implement something simple like this:
var counter = 0;
function uniqueId(){
return "Id" + ++counter;
}
You could just increment the id's with a number and some sort of string to begin the ID.
The span id's would be "a1", "a2" etc.
I'm guessing that the problem is that you're thinking later you'll be getting the same strings and will want to transform them in the same way, and then use these to find the original corresponding elements?
If so, you'll just need to sanitize your strings carefully. A series of regular expressions could help you map from invalid to valid characters, and make the capitals unique. For instance, you could transform "A" into "-a-", and "+" into "-plus-".
A carefully chosen scheme should guarantee that the chances of a collision (i.e. someone giving you a string that looks like an escaped version of another string) should be very small, and in any case, detectable immediately.

Categories

Resources