Javascript syntax to pass variables - javascript

Still muddling my way through Javascript, I'm trying to pass the contents of variable playnoyes to the long line of code below to decide whether or not to autoplay the flash movie, but doing it as I have below, the resultant line of code has the variable in quotes and therefore the code doesn't execute it as expected. My question is, how can I pass the variable so that the resulting line of code doesn't have the quotes around the variable value.
Many thanks, and sorry for the noobness of the question.
var playnoyes='true';
var testtext = "<script type='text\/javascript'>AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay=\""+playnoyes+"\"&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer');<\/script>";
alert (testtext);

Thats because you are explicitly adding the quotes:
change
nigel&autoPlay=\""+playnoyes+"\"&autoRewind=true'
to
nigel&autoPlay=" + playnoyes + "&autoRewind=true'

Try this:
var playnoyes='true';
var testtext = "<script type='text\/javascript'>AC_FL_RunContent ('codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width','320','height','220','id','HTIFLVPlayer','src','HTIFLVPlayer','flashvars','&MM_ComponentVersion=1&skinName=HTI_Skin&streamName=nigel&autoPlay="+playnoyes+"&autoRewind=true','quality','high','scale','noscale','name','HTIFLVPlayer','salign','lt','pluginspage','http://www.macromedia.com/go/getflashplayer','wmode','transparent','movie','HTIFLVPlayer');<\/script>";
alert (testtext);

Remove the two '\"' on each side of the variable, no?

Related

Adding JavaScript Function with arguments to an element from code behind

Hi Guys I've been dealing with an estrange thing while trying to pass string parameters to a JavaScript function from code behind, this is what I have actually in code behind which is wrong:
thumbnail = "<a href = 'javascript:RemovePirctureDetail(" + field1 + ",'" + tempname + "');' class='label label-default' rel='tooltip'>Remove</a>";
So this is the bad result I'm getting in the browser:
Remove
Meas that for some reason when I try to pass the string parameter, the html comes out bad formatted. The result should looks like this:
Remove
I tried already send the quotation marks like this /' from code behind, it did not work neither. How can I achieve this?
Thanks.
string thumbnail = "Remove";`
You need to use \ to escape the quotes inside, not /..
With javascript attribute I wouldn't use single quote, because it could be messy
Try to change in this way:
thumbnail = "Remove";
PS: Actually, I would NEVER use single quotes with attributes, it will cause an HTML validation error, HTML is not well formed with single quotes for attributes (although with inspection tools you see double quotes.. because inspection tools have the need to work with a well formed HTML, so if you want to see the real HTML of your page, view the source code (generally the option is on right-click on the page)

Using ColdFusion variable with line breaks in JavaScript

I have a text area that accepts line breaks. The content of that text area is saved in a coldfusion variable (lets call it #fieldVal#). So, my variable contents look like
textline 1
textline 2
Later on, I use that variable in JavaScript
document.all.fieldName.value = "#fieldVal#";
However, when the JavaScript hits the page, it looks like this:
document.all.fieldName.value = "textline 1
textline 2";
and the script breaks because the first line doesn't end in a semicolon.
I tried setting a JavaScript variable to the ColdFusion text then doing a replace(), but I still hit the same issue with the line not ending correctly.
I think I'm missing something obvious but I am just not seeing it. Can someone tell me what I'm missing here?
Use the JSStringFormat() function. It is designed to escape meta characters in JavaScript
Escapes special JavaScript characters, such as single-quotation mark,
double-quotation mark, and newline.
https://wikidocs.adobe.com/wiki/display/coldfusionen/JSStringFormat
document.all.fieldName.value = "#JSStringFormat( fieldVal )#";
If you're using ColdFusion 10+ or Lucee Server, use EncodeForJavaScript().
https://wikidocs.adobe.com/wiki/display/coldfusionen/EncodeForJavaScript
Option 1
document.all.fieldName.value = "#Replace(trim(fieldVal), chr(10) & chr(13), ' ', 'ALL')#";
Option 2 (Possible that the same issue occur in this too.)
Try using ToScript()
ToScript(fieldVal, valueVar)
Toscript initializes a variable with the coldfusion variable value and you can use like any JS global variable.
document.all.fieldName.value = valueVar;
Option 3 (If you need in HTML form)
Use coldfusion function ParagraphFormat() which changes line breaks into <br/>.
I stumbled upon a code snippet that worked. Its a bit...convoluted, but it works.
document.all.fieldName.value = "#Replace(Replace(URLDecode(Replace(Replace(URLEncodedFormat(fieldVal), "%0D", " ", "ALL"), "%0A", "", "ALL")),CHR(34),"", "ALL"),CHR(39),"", "ALL")#"

Href attribute using variable does not work

In JS, I am creating a link where the href attribute runs a function. The function simple opens an alert window with a given variable.
function alerter(str){
alert(str);
}
When I create the link, I use the following code.
var link = document.createElement('a');
link.setAttribute('href','javascript:alerter("hi")');
link.innerHTML = "Sauce";
If I do that, it works. However, what I want to do is alert a previously defined variable. But when i use
link.setAttribute('href','javascript:alerter(myvar)');
the popup no longer works. Also, is there a way to simply embed the alert in the href instead of writing a separate function?
It should be
link.setAttribute('href','javascript:alerter("' + myvar + '")');
You're using string concatenation to put myvar into that string. The way you had it, the whole thing is a string "javascript:alerter(myvar)"
Try this
link.setAttribute('href','javascript:alerter('+myvar+')');
The myvar variable has to be in the global scope. Be sure you can also access it as window.myvar.
for your first question use :
link.setAttribute('href','javascript:alerter(' + somevar + ')');
and for your second question use:
link.setAttribute('href','javascript:alert(' + somevar + ')');
cheers.
http://jsfiddle.net/xmBbw/2/
Just modify the onclick event of your anchor. If you are not going to link anywhere, you need not use the href attribute. Rather use onclick and assign any function you like to it. Besides, it's awfully bad practice to write javascript inside an html tag. Please don't.
A nice article regarding good practices

Pass variable to a javascript function in a c# code

I need to pass a variable to a javascript function,but I got a little trouble.
In the .cs file,I write like this:
string id = "someid";
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction("+id+")\">"));
I need to use the value of this id,but in the console.log(id),it just shows "object",not the "someid",what's the problem?
Look at the generated HTML:
<input type="button" onClick="myFunction(someid)">
You are generating a variable name when you want a string literal.
Add some quote marks.
Whenever you have a problem that manifests in the browser: Look at the code the browser is dealing with first. You should always start by determining if the JS you want is not working or if the server side code is not generating the JS you want.
just add '' around id as i did below will resolve your issue
this.Controls.Add(new LiteralControl("<input type=\"button\" onClick=\"myFunction('"+id+"')\">"));
Use the single quotes around id:
'"+id+"' (notice the single quotes then double quotes)
The problem is that it is looking for a saved variable on the page named someid, which is why you are getting the object in the console. Pranay Rana has a good solution which is to make sure the string someid is surrounded by single quotes.

How to print </script> as text using Javascript

Hi i am trying to assing this value to a variable in javascript but always get an error. I have no clue what is going on, my statement is something like:
var a = "</script>";
I tried everything but it does not work, what should i do instead??
You need to hide that from the HTML parser:
var a = '</' + 'script>';
If you don't, then the surrounding HTML parser — which knows nothing at all about JavaScript syntax — thinks that that's the end of the script block. After it sees the opening tag for the block, it literally scans for </script> (ignoring case of course).
(edit — though basically right I think, my claim that browsers are searching strictly for </script> is probably somewhat overstated; I suspect that they look for anything that's recognizable as a closing tag. The basic point still stands of course.)
I may be misunderstanding your question but if you are trying to output '' on the screen for the reader (human) to view, then you can use:
var a = "</script>";
Note that this won't work for executable scripts however - it won't be interpreted as a html/script tag.

Categories

Resources