How to write hashtag in JavaScript? - javascript

I have a function like this:
function tweet(){
window.open("https://twitter.com/intent/tweet?text=My text. #myHashtag"
);
}
But JavasScript stops at the # sign. How to write it?

Use %23 which an encoded form of #.
If this is coming from the user, you really should use encodeURIComponent before putting it into the query string.

Based on twitter doc, you can pass hashtags as &hashtags=
https://twitter.com/intent/tweet?text=My+text&hashtags=bransonpickel
https://dev.twitter.com/web/tweet-button/web-intent

Encode the (input) text:
let input = "My text. #myHashtag";
let baseUrl = "https://twitter.com/intent/tweet?text=";
// window.open(baseUrl + encodeURIComponent(input));
console.log(baseUrl + encodeURIComponent(input));

Related

Get substring after a word and before an filename extension

I have a URL in the following format:
https://res.cloudinary.com/xyzzz/image/upload/v1673615977/dealetePosts/hokhqmcmmkveqhxtr0nb.jpg
How to extract hokhqmcmmkveqhxtr0nb from this?
So extract contents between dealetePosts and .jpg
String position, followed by substring would work but is there an easier way?
This is what I have so far and works but is this the best way?
const publicID = dealPic.substring(
dealPic.indexOf("dealetePosts/") + 13,
dealPic.lastIndexOf(".jpg")
);
You can use something like a split and pop method to slash "/" & "." characters. Thats if you are always expecting the same type of url.
let url = "https://res.cloudinary.com/xyzzz/image/upload/v1673615977/dealetePosts/hokhqmcmmkveqhxtr0nb.jpg";
let key = url.split("/").pop().split(".")[0];
console.log(key);
I use the substring function and a regex to remove any extension (jpg, png, etc) and it'works even if dealetePosts changed to anyother name
const test = "https://res.cloudinary.com/xyzzz/image/upload/v1673615977/dealetePosts/hokhqmcmmkveqhxtr0nb.jpg"
function substr(str = ""){
const lastIndexSlash = str.lastIndexOf("/") + 1
return str.substring(lastIndexSlash, str.length).replace(/\.[^/.]+$/, "");
}
console.log(substr(test))
Another option that could be considered easier to read and understand is using regular expressions to match the text you want to extract. The following code will match the text between "dealetePosts/" and ".jpg" and return it as the first captured group:
const publicID = dealPic.match(/dealetePosts\/(.*)\.jpg/)[1];

Regex to separate an ID after a specific word

Can someone please help in splitting an ID after a specific word in a URL. I need to delete a specific ID from URL and insert a custom ID.
The url goes like : "/abc/mode/1234aqwer/mode1?query".
I need to replace 1234qwer by 3456asdf.
Example:
Input:
/abc/mode/1234aqwer/mode1?query
Output:
/abc/mode/3456asdf/mode1?query
One option is to .replace /mode/ followed by non-slash characters, with /mode/ and your desired replacement string:
const input = '/abc/mode/1234aqwer/mode1?query';
console.log(
input.replace(/\/mode\/[^\/]+/, '/mode/3456asdf')
);
This is the solution without using regex. Use ES6's replace instead:
url = "/abc/mode/1234aqwer/mode1?query"
replace = "1234aqwer"
replaceWith = "3456asdf"
console.log(url.replace(replace, replaceWith))

+ Sign removal javascript

Hi I dont know why + sign is removed and how to eliminate it's removing.
Sample code is presented:
var customer_number = $('cust_num');
var l_sParams = 'number='+customer_number.value;
alert(l_sParams);
var l_sURL = '/caller/send_sms';
new Ajax.Request(l_sURL, {parameters: l_sParams, method: 'POST',
onComplete:function(a_oRequest){
}.bind(this)
});
the alert displays ex: +1907727500
and if I print in Python it is printed without + sign like this ex:
_to_customer = self.request.post['number']
result: 1907727500 (without + )
Thank you
+ in a query parameter is the escape code for a space. You receive ' 1907727500', with the space.
Use %2B instead, or better still, have JavaScript quote your values properly
var l_sParams = 'number=' + encodeURIComponent(customer_number.value);
Strings containing a plus sign (or such special chars) should be urlencoded since it represents space in URLs. Use encodeURI() to do that.

How to convert signs in url/text to hex characters? (converting = to %3D)

With the script I'm making, jquery is getting vars from url parameter. The value that its getting is an url so if its something like
http://localhost/index.html?url=http://www.example.com/index.php?something=some
it reads:
url = http://www.example.com/index.php?something
If its like
http://localhost/index.html?url=http://www.example.com/index.php?something%3Dsome
it reads:
url = http://www.example.com/index.php?something%3Dsome
which would register as a valid url. my question is how can I search for = sign in the url variable and replace it with hex %3D with jquery or javascript?
Use the (built-in) encodeURIComponent() function:
url = 'http://localhost/index.html?url=' +
encodeURIComponent('http://www.example.com/index.php?something=some');
Are you looking for encodeURIComponent and decodeURIComponent?

Javascript replace query string + with a space

I'm grabbing the query string parameters and trying to do this:
var hello = unescape(helloQueryString);
and it returns:
this+is+the+string
instead of:
this is the string
Works great if %20's were in there, but it's +'s. Any way to decode these properly so they + signs move to be spaces?
Thanks.
The decodeURIComponent function will handle correctly the decoding:
decodeURIComponent("this%20is%20the%20string"); // "this is the string"
Give a look to the following article:
Comparing escape(), encodeURI(), and encodeURIComponent()
Adding this line after would work:
hello = hello.replace( '+', ' ' );

Categories