Javascript syntax error when passing parameters to a function - javascript

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();
}

Related

How to store variable and set the variable in field

I'm using Selenium 3.17.0.
I want to type a text from a javascript, but it doesn't work!
I have tried this (this types the text "undefined")
and this (this types the entire script as text)
but nothing works! maybe I'm doing wrong the javascript but I don't know, I'm new in all of this, please help!
btw this is my first post, sorry if I'm doing it wrong.
If I understand your question correctly, you are trying to generate a random string using javascript and then store the value in a variable. Then enter that variable value using type.
you have to use execute script command to run the javascript and target (your javascript) should be return "free text" + Math.floor(Math.random()*100) as shown in the below screenshot.

Print html to a surface to be copied

I stored an table's html as a text, using this code.
var Data = document.getElementsByClassName("result")[0].innerHTML;
I am able to observe the selected part using console.log, however, I wish to extract this data to be copied and used outside.
So I tried alert(Data), but it does not offer a good surface to copy the data (it does work though, however I cannot use right click on the pop-up window)
I also tried to programmatically copy the data to the clipboard, but it seems, it only works on selected text data.
Is there a better way to extract such data to be used outside ?
Note: I am using a firefox bookmark to execute javascript. But I expect the code to work also in the other browsers.
Edit: I tried the method suggested in the comments, however in firefox, I got an error.
document.execCommand(‘cut’/‘copy’) was denied because it was not called from inside a short running user-generated event handler.
So rather than copying with that command, printing to a surface seems a better choice, if possible. The linked question does not solve my issue.
Edit2: window.prompt did a much better job, however it rocked my world by pressing the text to a single line. I still should be able to parse it programmatically, but if there is a better answer, I wish to learn it.
Below is my solution to keep multiple lines.
It creates one temp 'textarea', then remove it after select()->copy.
function triggercopy() {
var target_obj = document.getElementById('test1');
var copy_text = target_obj.innerHTML; //replace with your actual data.
var hidden_obj = document.createElement("textarea");
hidden_obj.value = copy_text;
document.body.insertBefore(hidden_obj,target_obj);
console.log('prepare:' + copy_text);
hidden_obj.select();
document.execCommand("copy");
document.body.removeChild(hidden_obj);
console.log('already copied:' + copy_text);
}
Text3as
dfsadf
<a id="test1" onclick="triggercopy();">Text3as
dfsadf</a>
I found two methods best suit my interests.
First, window.prompt:
var Data = document.getElementsByClassName("result")[0].innerHTML;
function copyToClipboard(text) {
window.prompt("Copy data.", text);
}
copyToClipboard(Data)
This is a good method, taken from a suggested answer. This puts the data into a single-line text field. And in an interesting manner, when written without a function, executes document.write(Data) when clicked OK, this does not happen when written in a function as above.
Second, document.write:
var target = document.getElementsByClassName("resultTable striped")[0].outerHTML;
document.open('text/plain');
document.write(target);
I first tried to open a new tab with the desired content, however encountered with the issue of pop-up blockers and non-plain text html data (formatted html instead of the desired table html data). This solves both issues.

Anyway to know it is stored in savedsearch or not

I'd like to search using savedsearch.
Here my code snippet goes.
var searchresults = nlapiSearchRecord('item', search_id, null, null);
search_id is defined as parameter in text field.
This is suitelet script so if you couldn't find similar search_id in savedsearch then it throws exception.
To avoid this I'd like to check if there is any similar internal id in saved searches.
For instance if there are two saved searches which ids are customsearch1, customsearch2.
If search_id is 'cust' then it throws exception and script finished with error.
It shows this in script log
'That search or mass update does not exist.'
Looking forward to hearing from you soon.
Regard
You can do a saved search of saved searches. You could take the results and use regex to determine if there is a similiar one. Use trim plus regex.
You could prevent this by changing your search_id parameter to a List/Record of Saved Searches.
Any reason why it has to be a text field?

Why cant I get the text boxes to POST/GET when I submit

Here is the dom:
This is how I am submitting:
function createOrderItems(){
showCustomPanel(" Creating Order Items ");
$("#order_items")[0].submit();
return false;
}
Here you can see that the text field isnt getting picked up:
You need name attribute on it to get it included in params
To add more detail to the answer already given, If you don't give the input a name then it has nothing to assign the information to that is entered into the text box.
Think of it like a variable
= "stuff"; wont do anything
this = "stuff"; will store stuff into this. Then you can use that information later.
Not really how it works on the back end but it is a good way of understanding it.

Meaning of JavaScript form validation code?

I have to explain how a specific Javascript code validates a web form, but I am stuck with what some of the features do, most specifically this section of the code. I understand that the first line defines that the rest of the section should only run if the field Field1 of the form ExampleForm is left empty, but I do not know what purpose the rest of the code serves. All I know is that msg is a variable created earlier in the document with an empty default value, and that result is another variable with a default value of true. Can anyone help me out by explaining what each line does?
if (document.ExampleForm.Field1.value=="") {
msg+="You must enter your name \n";
document.ExampleForm.name.focus();
document.getElementById('Field1').style.color="red";
result = false;
}
In plain english:
If the document form field value is equal to an empty string, set the error message to msg, then focus on the element, and give is a red color so the user knows it's an error, and set the result to false, for whatever you're going to use that for later in your code/function.
So this would in part depend on what other code is on the page. For example document.ExampleForm is not part of the DOM and seems to be something someone kludged onto your page.
Overall I would say this is pretty bad code that makes a ton of assumptions that won't necessarily hold up written by someone who doesn't understand in-browser javascript very well, but let's go with it
//if the value in this variable is falsy (false, empty, or 0)
if (document.ExampleForm.Field1.value=="") {
//Append this to the msg string. Note \n is usually used
//to indicate "new line" but wont' do anything on the web since that's not how line breaks
//work on the web
msg+=”You must enter your name \n”;
//Invoke the `focus` field on this variable. From the context I assume this is
//a DOM node so you are basically focusing it in the browser
document.ExampleForm.name.focus();
//Set the font color of '#Field1' to red
document.getElementById('Field1').style.color=”red”;
//Presumably result is something that tells you true/false did the validation succeed.
//set it to false to indicate failure.
result = false;
}
My guess about what document.ExampleForm is that it depends on some undocumented behavior of an old browser to add anything with id=ExampleForm to the document element. But really I have no idea. Maybe you have some code elsewhere that creates that variable. Either way its a horrible idea and should get someone yelled at.

Categories

Resources