Angular add extra variable on Img at end %E2%80%8B - javascript

http://myimage.png%E2%80%8B
I have just copy paste this img url but it added some wiered data on end and image does not show . but on code it looks perfect .
I have tried something in Angular like
var imageSafeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.imageLink);
this.imageLink = imageSafeUrl['changingThisBreaksApplicationSecurity'];
but still it adds

The data is url encoded so it can be typed safely in an address bar and interpreted by the browser/server
%E2 = â
%80 = `
%8B = ‹
A table to help you interpret the encoded characters is available at https://www.w3schools.com/tags/ref_urlencode.asp
So together you have copied http://myimage.pngâ`‹

Added "?" after link now Extra data is set as query parameter
<a title="GreenPop" href="http://myimage.png?" >

Related

Twitter web intent URL encoding not working

I have no idea what I'm doing wrong here. I'm trying to encode my URL with javascript. But the URL never gets put into the tweet. I think it has something to do with some parameters in my URL having spaces.
https://twitter.com/intent/tweet?text=40%25%20Off%20Prom%20Tuxedo%20Rental&url=http%3A%2F%2Fexample.com%3A5757%2Fcoupon%3Fref_name%3DTest%20Name%26school%3DTest%20School
If I take out the %20's from my URL then it works...
https://twitter.com/intent/tweet?text=40%25%20Off%20Prom%20Tuxedo%20Rental&url=http%3A%2F%2Fexample.com%3A5757%2Fcoupon%3Fref_name%3DTestName%26school%3DTestSchool
But I need to keep those spaces in there.
This is the javascript code I have right now...
var text = encodeURIComponent("40% Off Prom Tuxedo Rental");
var couponURL = encodeURIComponent("http://example.com/coupon/?ref_name=Test Name&school=Test School");
var twitterURL = "https://twitter.com/intent/tweet?text=";
var twitterURL = twitterURL+text+"&url="+couponURL;
In this situation, a percentage sign equals %25, so if you want to include the spaces in your tweet link, use %2520 instead of %20. So wherever you were going to use %20, use %2520 instead.
var text = "40% Off Prom Tuxedo Rental";
text = text.replace(/\s/g, "%2520")

How to adjust a link using jQuery or Javascript

What I'm trying to do is to change the last character of a link using jQuery or Javascript.
The link that the client will input into the CMS looks something like this: https://www.dropbox.com/sh/wbc1ewhfg1jttpr/AADLfZKlfOBs5e_ueAkzffKRa/SamplePDFDownload.pdf?dl=0
What I'd like to do is to set the website to take this link and replace the '0' on the end of the link with a '1'.
Does anyone know how this could be done automatically?
Any help is appreciated,
Tom
As easy as that:
var url = document.getElementById("id of element").href;
url = url.substring(0, url.length-1);
url = url + "1";
document.getElementById("id of element").href = url;
Probably google first about string functions before asking...
If the pattern of the url stays the same (the parameter dl=0) then you can simply use the .replace()-function:
var url = 'https://www.dropbox.com/sh/wbc1ewhfg1jttpr/AADLfZKlfOBs5e_ueAkzffKRa/SamplePDFDownload.pdf?dl=0';
url = url.replace('dl=0', 'dl=1');

how to escape single quote in dynamically created HTML tag content

In my javascript code,
I have a variable which have a string. String contains ' or quote in it. Example
var name= "hi's";
I am creating a link dynamically in a code. where it is written as a string i.e a variable content will be used dynamically to create a link on html page.
content= '<a onclick="javascript:fun(\'' + name + '\');">'
Here it is giving problem that quote in variable name completes the string in content. and hence rest portion of content is not recognised..
similar problem arises if var name = 'hi"s';
i.e. if double quote is present in it.
plz help
This is how you would create an anchor properly and completely avoid the need to escape anything.
var name = "Hi's",
anchor = document.createElement('a');
// should have an href
// links will be displayed differently by some browsers without it
anchor.href = '#';
// using onclick for pragmatic reasons
anchor.onclick = function() {
fun(name);
return false;
}
anchor.innerHTML = 'hello world';
// later
mydiv.appendChild(anchor);
Btw, the onclick attribute shouldn't start with "javascript:" at all; that's already implied.
Update
If you're still interested in the inline version, variables need two steps of encoding:
The first is to serialize the variable properly; I would use JSON.stringify() for that
The second is HTML escaping; the simplest form is simply to replace double quotes with their proper encoded values.
For example:
var content = '<a href="#" onclick="fun(' +
JSON.serialize(name).replace(/"/g, '"') + ');">hello</a>';

Encoding URL (including characters like &) using jquery or native javascript function?

I have one one hidden paramter in form whose value is
custAddress=CustomerAddress.do?fisrtName=scott&lastName=Miles
I want to encode it before sending it and so that characters like & can be replaced with %26 etch
i tried using javascript built-in encodeURI("urlToencode") but does not encode characters like &?
Try this code line,
encodeURIComponent("fisrtName=scott&lastName=Miles");
Use https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/encodeURIComponent
You need to call that on each dynamic part (name and value) of the URL query string. So the question is what is the URI component in custAddress=CustomerAddress.do?fisrtName=scott&lastName=Miles That doesn't really look like a URL because you have the = before the ?
The most sense that I can make is that the full URL is something like
http://myserver/file.do?custAddress=CustomerAddress.do?fisrtName=scott&lastName=Miles
In which case, you should build your URL like
var custAddress = "CustomerAddress.do?fisrtName=scott&lastName=Miles";
var initialPath= "/path/to/file.do?";
var url = initialPath + "custAddress=" + encodeURIComponent(custAddress);
Since you mentioned jQuery, you can use a $.param, looks cleaner and does the encoding for you, and you can give it multiple query parameters at once
var url = initialPath + $.param({
custAdrress: custAddress,
otherParam: "paramVal",
// Both the param name and value need to be encoded and $.param does that for you
"funny Name & Param": "funny & value ="
});

How to encode UTF8 characters into Base64 in JavaScript

I tried like this when handling chinese, but the output is messy code garbled:
var a = "你好";
undefined
a
"你好"
a = unescape(encodeURIComponent(a));
"ä½ å¥½"
a
"ä½ å¥½"
compared to handling English:
var a = "Hello";
undefined
a
"Hello"
a = unescape(encodeURIComponent(a));
"Hello"
Here is my whole code:
var content = $("div#test").html();
content = unescape(encodeURIComponent( content ));
content = window.btoa(content);
content = "data:image/svg+xml;filename:{{ request.session.access_token.uid }}.svg;base64," + content;
UPDATED:
What I am trying to do is encoding a generated SVG as data url for user to download. I solved this problem by converting SVG to canvas (using CanVG) first and use html2canvas to generate base64 ... finally it works ...
Try roundtripping this, and it works fine:
decodeURIComponent(escape(window.atob(window.btoa(unescape(encodeURIComponent("‌​東京"))))))
Whether or not some intermediate result appears to be garbled is not relevant.
The problem with the window.btoa(decodeURIComponent(escape(unescape(encodeURIComponent("東京"))))) in one of the comments is the calls are not balanced and not in the right order.

Categories