Passing in a String argument from Wordpress placeholder - javascript

I'm currently using Wordpress event manager. There is basically a placeholder feature where when using Wordpress in which text from say the "Excerpt" text area will be saved into a placeholder say #_EVENTEXCERPT. The problem I am having is that when I pass this in as a String argument it yields an error because of the line breaks (I'm guessing)
Link to console error
jQuery("#event-excerpt").append("<p id ='test'>#_EVENTEXCERPT</p>");
Is there any way I can get around this?
Update: Link to console after fixing quote mismatch Updated console link

Try that:
jQuery("#event-excerpt").append("<p id ='test'>#_EVENTEXCERPT</p>");
And also you need to replace the line breaks, something like that:
var test = "<p id ='test'>#_EVENTEXCERPT</p>";
test = test.replace("<br>", " ");
jQuery("#event-excerpt").append(test );
I dont know if this solve the problem for you, because I just replace the line breaks with spaces.

Related

How to prevent new line caused by \\n in %WINDIR%\\system32\\netsh.exe in javascript

Is there a way that I can prevent the new line caused by '\n' in:
%WINDIR%\\system32\\netsh.exe
I want to show this string in a html page and I use javascript to show it. However, I get:
%WINDIR%\system32 etsh.exe
Also, I get this string using a python template and I already tried to replace it with various combinations of '\', '/', and 'n' in python and still no luck!
I appreciate if someone can provide some hint.
Two options I can think of: first is what I recommended in my comment: prepending your string with an r. So using r'%WINDIR%\\system32\\netsh.exe', instead of '%WINDIR%\\system32\\netsh.exe'.
Alternatively, if that doesn't work, you can try using pathlib. Something like this:
from pathlib import Path
path_str = str(Path('%WINDIR%\\system32\\netsh.exe'))
This worked fine for me:
var text = "%WINDIR%\\\\system32\\\\netsh.exe";
document.getElementById("result").innerHTML = text;

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")#"

Javascript syntax error when passing parameters to a function

I'm not sure how to code this as I may be running into the server side rendering versus client side. It's been a while since I did something like this therefore your input is very helpful.
I'm using a modal popup extender to display a message that is fetched from a database and is rendered in label inside a gridview. My ideas was to use the JavaScript function that is responsible for showing the modal popup. Initial method was to send two params to the function, one being the "this" keyword and the other the message itself as rendered by the .net <%# Eval("Message") %>. This however gave me all kind of syntactical trouble. It should have been as simple as OnClientClick='showComfirm(this, <%# Eval("Message") %>); return false;' but wasn't.
Then I thought I can just use the document.getElementById(ct100_ContentsPlaceholder1_hEmailMessage).value. This works but what doesn't is, assigning the value of the label in the popup. Here some code:
function displayInfo(source, message){
this._source = source;
var emailBody = document.getElementById("ct100_ContentsPlaceholder1_hEmailMessage).value;
document.getElementById("ct100_ContentsPlaceholder1_lblPopUpMessage").value = emailBody; // This is where I'm trying to assing text property.
this._popup - $find('mdlPopup');
this._popup.show();
}
The "message" parameter is what I described at the top as my initial method, which I still would like to make it work.
Anyway, lot of explanation, but would like your input as to why I get syntax error in my initial attempt, as well as why am I not able to assign the value to the text property of the lblPopUpMessage.
Thanks,
Risho
UPDATE
The javascript errors I was getting were due to the text which the varialble "message" contains. The varialbe contains plain English text such as a email message, with upper case, lower case letters, exclamation marks, commas, periods, appostrophies, double quotes, space, etc. How do I know this? I've removed all the text save one random word in the message cell in the database and the errors went away. Still could not display the single workd message though.
So is there a workaround for this?
Thanks, R.
You copy + paste the code ? If so, you got a syntax error. This is the good code:
function displayInfo(source, message){
this._source = source;
var emailBody = document.getElementById("ct100_ContentsPlaceholder1_hEmailMessage").value;
document.getElementById("ct100_ContentsPlaceholder1_lblPopUpMessage").value = emailBody;
this._popup - $find('mdlPopup');
this._popup.show();
}

Jquery taconite selector with character that needs to be escaped

I'm using the jquery taconite plugin to make an ajax request that will replace a certain element in my page, however the element has an id like "email.subject"..
I can select it just fine if I do '$("email\\.subject")', but when I try to use the taconite plugin like this:
<taconite>
<replaceWith select="#email\\.subject">
JUCA
</replaceWith>
</taconite>
The plugin log says:
[taconite] No matching targets for selector: #email\\.subject
How can I make this work?
Ok this is what i did. It is not working for me though. (I did not go through the entire source). But it will point you in the right direction.
The problem really is that in the jquery.taconite.js file, around line 152 (if you are looking at the latest version!) you can see:
var q = cmdNode.getAttribute('select');
var jq = $(q);
if i add an alert to the above statement to find out the value of jq it says: [Object object]. But this works as long as it contains no .
The problem is that the author of taconite is not checking for . from the "select" attributes value. The following code works for me when i tried it isolated in a simple js file. But when i use the same in the jquery.taconite.js file it does not work. Needs more tweaking around?
var x = cmdNode.getAttribute('select');
alert(x); //Shows what you have entered in <replaceWith select="#email.subject"> i.e "#email.subject"
var q = x.replace(/\./g, "\\\\\."); //Searches for a . in the string and escapes it! So now it becomes: "#email\\.subject"
alert(q) //Alerts #email\\.subject ... Great! Works fine till this point!
var jq = $(q);
alert(jq[0]); //Says "undefined"!!!! This is where i got stuck! Why does it say undefined??

Categories

Resources