I have recently started writing some scripts for Google Spreadsheets. I have no experience with Javascript though and I have question that is concerning a (as I suppose) basic issue.
I would like my script to insert data shown below into a cell in a sheet. How should I encode it to make it work?
komorkaLinku.setValue("=HYPERLINK("http://www.some.link/some/data"+variable+"something","something")");
I had tried several ways but none of them worked.
You are trying to include quotes inside quoted text. There are a couple of ways to do that.
Use single quotes inside double quotes, or vice-versa.
komorkaLinku.setValue('=HYPERLINK("http://www.some.link/some/data'+variable+'"something","something")');
Use escaped single quotes.
komorkaLinku.setValue('=HYPERLINK(\'http://www.some.link/some/data'+variable+'\'something\',\'something\')');
As #ScampMichael comments, it would be a better choice to use setFormula() in this case. You would still need to handle embedded quotes properly.
Related
I write a lot of Tag Manager tags, and I prefer to write them in the VScode before putting them into the TM. The TM uses double curly brackets as a notation of its variables (ex. {{Page URL}}, {{Clicked Elements}} etc.) I would love to set the VSC to recognize such notation as a variable and not consider it an error. I've googled extensively but either I cannot phrase the question correctly or no one faced this problem before. Any help will be really appreciated.
I suggest you set language as Django HTML, after installing some extension like this one https://marketplace.visualstudio.com/items?itemName=batisteo.vscode-django
I agree that it can be useful for complex code, while you loose the auto-completion feature for variable names in Google Tag Manager when you do so.
I am trying to pass a few variables from my php to the javascript and its working fine except for when I add a particular variable to the function call. There are 3 variables being passed all of which are related to a youtube video. the ID is being retrieved from a database, and the other two (title and description) are being retrieved using the ID from the youtube api.
<a href="#" class="list-group-item" id="{{$vidID[$i]}}" onclick="updateVid('{{$vidID[$i]}}', '{{$title[$i]}}', '{{$desc[$i]}}test')">
I am using hogan templating so the {{}} is the same as php tags with an echo. The problem only seems to occur when I add in the description variable to the call. Without it, the other two work perfectly fine. After doing some research it seems this is caused by an invisible character but I retyped my code to make sure and my code wasn't the problem. The developer console shows that problem is occurring on the last line of the description variable so it seems that the description is being retrieved with an invisible character from the api. I am just trying to do a simple alert with the description in the js function. How can I get rid of this character?
Without seeing the actual values, the most obvious problem would be that characters in your content are breaking the html and / or the javascript function call.
Echoing variables directly in html like that is a bit tricky as you need to escape for the javascript but also for the html. In this case you can probably use:
... '{{htmlspecialchars(json_encode($desc[$i]), ENT_QUOTES)}}test' ...
The json_encode call will probably get you an extra pair of double quotes around the string.
However, note that the best way to get your variables to javascript is to do that directly in a script block - without html in the middle - using json_encode in php. Then you can be sure any data can be passed without problems and you can decode it in javascript to get your structure back (in case of arrays and objects).
I have the following link being created dynamically in a legacy .asp application.
<a href='javascript:OpenIncompleteProposal(<%=iProposalID%>, <%=Server.UrlEncode(Session("Security_UserFullName"))%>)'><%=sClientName%></a>
When this renders it renders with no quotes around either parameter, i want the second parameter surrounded by single quotes. i know it should be easy but escaping the strings and wrapping the globalsession variable is causing me some trouble.
In my rails app, I'm adding text dynamically to the page using something like this
$('.container').append('<div class="test"><%=description%></div>')
The issue is that sometimes the ruby variable "description" contains single quotes (for example, it might container the word "I'm"), which leads to errors when the page tries to render the text.
What's the best way to avoid this problem by escaping quotes in description?
Checkout Rails' JavaScript Helper, in particular escape_javascript(javascript)
Escapes carriage returns and single and double quotes for JavaScript
segments.
Also available through the alias j(). This is particularly helpful in
JavaScript responses, like:
$('some_element').replaceWith('<%=j render 'some/element_template'
%>');
I have JS calling remote server through AJAX. The response contains something similar to this
<script>alert(document.getElementById('some_generated_id').innerHTML; ... </script>
User copies the response and uses for own purposes. Now I need to make sure that not a single browser runs the code when I do this:
var response = '<scrip.....';
document.getElementById('output_box').innerHTML = response;
Same should apply to any HTML tags. I know that .text() from jQuery will do exactly what I need:
var response = '<scrip.....';
$('#output_box').text(response);
I am looking for any solutions, including, but not limited to: escaping special characters, however displaying them correctly; adding zero-width space to tags (has to be efficient); outputting in parts. Has to be pure JS.
If you're using a server-side language there is probably a method to escape special characters.
In PHP you could use htmlspecialchars(), it will convert certain characters that have significance in HTML to HTML entities (i.e. & to &).
They will still display correctly and you'll be able to copy and paste the text, but the javascript shouldn't run.
If you need a pure javascript solution for this, someone has answered that here https://stackoverflow.com/a/4835406/15000