<\/script>')

C # StringBuilder - how to avoid this line:

<document.write("<SCR"+"IPT TYPE='text/javascript' SRC='"+"http"+(window.location.protocol.indexOf('https:')==0?'s':'')+"://"+gDomain+"/"+gDcsId+"/wtid.js"+"'><\/SCR"+"IPT>");

      

I need to escape the line above to add the whole thing to the StringBuilder, but so far I must be missing something because the line termination is wrong ...

0


source to share


4 answers


string x = @"<document.write(""<SCR""+""IPT TYPE=""'text/javascript' SRC='""+""http""+(window.location.protocol.indexOf('https:')==0?'s':'')+""://""+gDomain+""/""+gDcsId+""/wtid.js""+""'><\/SCR""+""IPT>"");";

      

The @ prefix simplifies traversal. You just need to rotate each "in".

You will find your program much easier to maintain if you store JavaScript in an external file. I am assuming you are using a StringBuilder so you can mix the script constant bit with multiple dynamic values? You can write it to a file, but for dynamic values, enter escapes:



var fromCSharp = {0};

      

Then, at runtime, load the JS file and pass it to string.Format as a format string along with values ​​to replace each occurrence of {0}, {1}, etc. You only need to load the format string from the file once and store it in the cache.

Also, if the values ​​you are inserting into JavaScript are themselves string literals, then you will need to escape them according to JavaScript syntax.

+4


source


You should try something like this:

@"<document.write(""<SCR""+""IPT TYPE='text/javascript' SRC='""+""http""+(window.location.protocol.indexOf('https:')==0?'s':'')+""://""+gDomain+""/""+gDcsId+""/wtid.js""+""'><\/SCR""+""IPT>"");"

      



When prefixing a string literal with @, the only possible escape is to double the "caracter.

Hope for this help.

+6


source


I think you are mixing what is JavaScript

and what is C#

. Could you please tell us the line you are trying to achieve ...

eg

window.location.protocol.indexOf('https:')

JavaScript

but presumably

gDomain

and gDcsId

are variables from your method C#

perhaps it:

"<SCRIPT TYPE='text/javascript' SRC='"+"http"+"(window.location.protocol.indexOf('https:')==0?'s':'')"+"://" + gDomain + "/"+ gDcsId+ "/wtid.js"+"'></SCRIPT>")

      

0


source


The line at the beginning is what I want for sure ... (I am not mixing JavaScript with C # - I just need to add a line to the C # StringBuilder which coincidentally contains some JavaScript)

Its an external script that I have to place on the page using a StringBuilder (for various reasons).

I have no way of knowing if any changes to the script will fail, so I have to include it like ...

This is just 1 line from the total script, but most of the other lines I managed to avoid correctly, and they are included as needed ...

0


source







All Articles