Built up string causing a syntax issue - javascript

I have a c# function that builds a string which in turn is used as a hyperlink to another page. However, with some strings with single quotes it is causing a javascript error as shown here:
I'm calling the javascript function in the code behind as so
linkFullMatch.NavigateUrl = "javascript:showFullMatches(" + sb.ToString() + ")";
the javascript is on the aspx function as so:
<script>
function showFullMatches(url) {
window.open(url, "_blank", "height=344,width=1100,scrollbars=yes,resizable=yes,toolbar=no,location=no,status=no,menubar=no,left=580,top=194");
}
Any help would be greatly appreciated. Any string that doesn't have a single quote in works fine and the page link opens as requested.
Rob

You need to add an additional layer of quote marks to make the sb.ToString() value an JS string. Adjust your call like:
linkFullMatch.NavigateUrl = "javascript:showFullMatches('" + sb.ToString() + "')";
Note the additional ' marks.

Related

what is proper syntax for regex to get specific text from GTM attribute.response

I used Google Tag Manager to create a custom data level variable to get the content of an ajax form. The result is in the attributes.response that looks like:
response:"{\"current_url\":\"https:\\/\\/domainname.com\\/ +
"manufacturer\\/category\\/model-number\\/\",\"h" +
"tml"\":{\"cart_status_528\":\"\\n <div id=\\\"s" +
...
"<a href=\\\"https:\\/\\/domainname.com\\/manufacturer" +
"-name\\/long-store-category-name\\/model-number-x\\/\\" +
"\" class=\\\"ty-product-notification__product-name\\\"" +
">PRODUCT-NAME THAT I WANT<\\/a>\\n " +
...
" <p><\\n more escaped html content +
}"
I am trying to extract/parse the attribute.response to retrieve the PRODUCT-NAME text. I have tried the following which matches in regexr. But, GTM keeps complaining there is an error in my javascript at the double quote symbol. What am I missing? Or is there a cleaner way to retrieve the text? Thanks
function() {
var regex = (?<=product-name(.|\n)*">)(.*)(?=<\\\\\/a);
var attributesResponse = {{attributes.response}};
if(regex.test{{attributesResponse}}
var ProductAddedToCart = regex.exec(attributesResponse)[1];
return ProductAddedToCart;
}
return false;
}
First of all, please read the top answer here: RegEx match open tags except XHTML self-contained tags
Secondly, your JS has many problems. Even the SO code highlighter indicates it. See some examples of how regex is used in JS.
The proper way to solve your task, however, would be adding a dataLayer push with the proper response details neatly stored in a dataLayer object. You would normally ask your front-end developers to add a push in their response callback. It should be trivial for them to tackle. You can read more on DL here.

Multiple attribute on href using onclick

I Tried this code to get multiple value in href but it does not work. any problem on this one ?
Print
You are missing a + sign between a string and a value.
The error is between this two
document.getElementById('CUS_CODE_MX').value '&AGE='
Correct format
document.getElementById('CUS_CODE_MX').value + '&AGE='
Every time you join a value and a string, you need a + sign
Even if you are joining two strings
'Hello'+ 'World'
Pliss avoid long js as an inline atribute. I will recommend you call a function as the onclick attribute.
Hope this helps :)
Print
It's better to use external script for that rather than inline format. And just add missing + to your code. Also, using variables would clean up the code.
function func() {
var CUS_CODE_MX = document.getElementById('CUS_CODE_MX').value;
var AGEID = document.getElementById('AGEID').value;
this.href = 'printsales.php?CUSTOMERID='+CUS_CODE_MX+'&AGE='+AGEID;
}
Print

Javascript parameter passing single apostrophe

I'm inserting content with js, that includes an onclick call to a function. This function passes a parameter which contains a database entry, which could contain a ' .
var action = 'Share';
Trouble is that when name contains a single apostrophe it breaks the function call. I've tried doing a string replace on name to replace ' with ' but this seems to still be converted back to a ' by the browser.
Any idea how I can get around this?
Use escape() or after JavaScript version 1.5. use encodeURI() or encodeURIComponent() instead.
Don't write code by mashing strings together with other code. You've got JavaScript inside HTML inside JavaScript and it is a recipe for headaches.
Use DOM manipulation instead.
var a = document.createElement('a');
a.href = "#"; // You should use a button instead of a link to the top of the page
a.className = "facebook-share";
a.addEventListener('click', function () {
facebookWallPost(name);
});
a.appendChild(
document.createTextNode('Share');
);

Javascript code inside var

I need to add some HTML and Javascript code inside a javascript variable to write them later in my .html page
example of what i tried to write in my Javascript page
var footer = "<footer><script>
if(isMobile.any()){
document.write('<a href="twitter:///user?screen_name=twitter">');
} else {
document.write('<a href="https://twitter.com/twitter">');
}</script></footer>";
and the way im writing it in my HTML document
<script>document.write(footer);</script>
but im getting errors inside my javascript page caused by the javascript code inside the variable
sorry i'm new in javascript and thanks in advance
Strings can't go across multiple lines without proper handling; embedded quotes need to be escaped too; this is by no means the cleanest way, but one suggestion could be:
var footer =
"<footer><script>" +
"if(isMobile.any()) {" +
"document.write('<a href=\"twitter:///user?screen_name=twitter\">');" +
"} else {" +
"document.write('<a href=\"https://twitter.com/twitter\">');" +
"}" +
"</script></footer>";
Also note that you use document.write inside script tags, when your literal script string has that tag. You need it for that piece of script, but you might consider your overall output.
You need to escape the quotes(") inside the string like \":
var footer = "<footer><script>
if(isMobile.any()){
document.write('<a href=\"twitter:///user?screen_name=twitter\">');
} else {
document.write('<a href=\"https://twitter.com/twitter\">');
}</script></footer>";
var footer = "<footer><script>
if(isMobile.any()){
document.write('<a href='twitter:///user?screen_name=twitter'>');
} else {
document.write('<a href='https://twitter.com/twitter'>');
}</script></footer>";
as another answer already pointed out, when the second double qute is encountered, it's considered as the end if the string, you've to either escape it, or use single quotes throughout the string, as above.
This is a slight modification to #Grant Thomas's answer
var footer = "<footer>\x3Cscript>\
if(isMobile.any()){\
document.write('<a href=\"twitter:///user?screen_name=twitter\">');\
} else {\
document.write('<a href=\"https://twitter.com/twitter\">');\
}\x3C/script></footer>";
You can read up about why you need to use \x3c here Link

FCKeditor, removing stuff

I am using FCKEditor in a CMS and need to post some javascript code in the editor.
This is stored in my database but it removes the + sign from this javascript code:
function _check(val){
loadFragmentInToElement('captcha_check.php?val='+val,'captcha_div','');
}
Now why would it remove the + sign?
I've tried using + and %2B but then it posts + and %2B instead of a + sign.
No, outside parties will not be able to access this to post stuff.
Edit....
The form with the editor is submitted using a javascript function
called submitform. The editor content is passed as an object called
noofeditor. I see where it 'escapes' the code retrieved from
the editor using this:
if(noofeditor){
var editorArray=noofeditor.split('::');
for (l=0;l<editorArray.length;l++){
strData += "&"+editorArray[l]+"="+escape(FCKeditorAPI.GetInstance(editorArray[l]).GetXHTML());
}
}
Is there a way to prevent it from escaping the + sign?
How about the following:
function _check(val){
var path = 'captcha_check.php?val='+val;
loadFragmentInToElement(path,'captcha_div','');
}

Categories

Resources