I've come across a problem I can't seem to solve. I am setting href
on an a
using Ajax. data.gameName
may be for instance The Witcher 3: Wild Hunt - Blood, Wine and Davis Jr.'s Underpants or perhaps Counter-Strike: Global Offensive. The thing is, example.net only allows a maximum of 40 characters in their searches.
How do I reduce data.gameName
, preferrably by cutting words on it's ending till it's 40 characters or less? So that
The Witcher 3: Wild Hunt - Blood, Wine and Davis Jr.'s Underpants
becomes
The Witcher 3: Wild Hunt - Blood, Wine
rather than
The Witcher 3: Wild Hunt - Blood, Wine a?
<span id="hej"><a id="videoLink" href="">This becomes a search link</a></span>
<script>
$.ajax({
url: "game.php",
type: "post",
async: true,
dataType: 'json',
success: function(data){
$("#hej").find("#videoLink").attr("href",'http://www.example.net/search?q=' + data.gameName);
/* Cut data.gameName properly here */
}
});
</script>
Any hints, tips or guidance will be much appreciated. Thanks in advance.