When I run it shows me the meaning of "Undefined" in Google.
When I run it, it performs a Google search for the word "Undefined".
function search(){
var x = document.getElementById("search").value;
const url = "https://www.google.com/search?q="+ x +"&oq="+ x +"&aqs=chrome..69i57j69i58.1760j0j7&sourceid=chrome&ie=UTF-8";
var win = window.open(url);
}
If the #search field cannot be found document.getElementById() returns undefined, which is used as part of the search query.
You can write a function like this, which will allow you to pass in a value to be searched.
function search(query){
window.open("https://www.google.com/search?q=" + query)
}
Or stick with your code but set a default value in the event that the selector does not return a match
function search(){
let x = document.getElementById("search").value;
if(x){
const url = "https://www.google.com/search?q=" + x
let win = window.open(url);
}
else {
console.log("No elements had the search id")
}
}
I'm fairly new to CRM development and I'm trying to customize my account form to Capitalize any text field at onChange. I'm currently working with this function that I found online:
function UpperCaseField(fieldName)
{
var value = Xrm.Page.getAttribute(fieldName).getValue();
if (value != null)
{
Xrm.page,getAttribute(fieldName).setValue(value.toUpperCase());
}
}
However, when I change a value in my test account it tells me that the method getValue() is not supported. Everything I've found tells me to use getValue(). Im at a loss.
Any help would be appreciated.
Thanks
If you're getting a getValue is not supported error, double check that the value for fieldName is actually a field on the form. It's best to code more defensively, like this:
function UpperCaseField(fieldName)
{
var attr = Xrm.Page.getAttribute(fieldName);
if (!attr) {
console.log(fieldName + " not found");
return;
}
var value = attr.getValue();
if (value != null)
{
attr.setValue(value.toUpperCase());
}
}
Update: When you connect your fields to JS functions via the form editor, CRM passes an event context as the first parameter. Here's what the code would look like in that case:
function UpperCaseField(context)
{
var fieldName == context.getEventSource().getName();
var attr = Xrm.Page.getAttribute(fieldName);
if (!attr) {
console.log(fieldName + " not found");
return;
}
var value = attr.getValue();
if (value != null)
{
attr.setValue(value.toUpperCase());
}
}
Here's more info about the context: https://msdn.microsoft.com/en-us/library/gg328130.aspx
Replace line
Xrm.page,getAttribute(fieldName).setValue(value.toUpperCase());
with line
Xrm.Page.getAttribute(fieldName).setValue(value.toUpperCase());
Also please provide a screenshot that shows how you use/register this handler.
I have some javascript code which calls a servlet with parameters based on the selected option from a SELECT list. It is possible that there will be no options in the selectCampaigns(account) so there is a try and catch.
The code in the catch should be called if,
var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
fails. This works in both chrome and IE but in firefox it gives a TypeErrror,
TypeError: selectCampaigns.options[selectCampaigns.selectedIndex] is undefined
which does not trigger the catch. May I have some suggestions on how to handle this? (in addition firefox does not show any of the alerts that I have inserted for debugging).
function selectcampaign(account) {
alert('alert1');
var selectCampaigns = document.getElementById("campaignSelect");
var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart";
try {
var selectedCampaign = selectCampaigns.options[selectCampaigns.selectedIndex].text;
urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account + "&campaign=" + selectedCampaign;
} catch(err) {
alert(err);
urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
} finally {
window.location.href = urlstrg;
}
};
You should better test the value instead of dealing with errors.
function selectcampaign(account) {
var selectCampaigns = document.getElementById("campaignSelect");
var urlstrg = "http://localhost:8080/SalesPoliticalMapping/OrgChart?account=" + account;
if(selectCampaigns.options[selectCampaigns.selectedIndex]) {
urlstrg += "&campaign=" + selectCampaigns.options[selectCampaigns.selectedIndex].text;
}
window.location.href = urlstrg;
};
I'm having really strange problems with my code using localStorage...
I would post code either here or in jsfiddle but for it to work I need a bunch of resources and for some reason won't display correctly on jsfiddle.
For an example, you can view the webpage I have it hosted at: http://spedwards.cz.cc/new.html
When you check one (can be any value but lets say 1 for this purpose) of the checkboxes for any hero, click Generate (in the last section) and hit refresh, all of the heroes have their activity checked even though only the one that was checked prior to the refresh should remain checked.
When checking localStorage in the console, only the checked one will have true and all the others will be on false as well which makes it weird. If someone can explain why it's doing this and/or explain an error that I've obviously missed.
Below I will post some of the functions.
Storing everything:
function storage() {
var username = $('#username').val();
var password = $('#password').val();
if (typeof(Storage) != "undefined") {
// Store user's data
window.localStorage.setItem("username", username);
window.localStorage.setItem("password", password);
$.each(heroes, function(index,value){
window.localStorage.setItem("heroActive" + index, $('input#isActive' + index).is(':checked') );
window.localStorage.setItem("heroLevel" + index, $('input#level' + index).val() );
window.localStorage.setItem("heroPrestige" + index, $('input#prestige' + index).val() );
});
} else {
// Browser doesn't support
alertify.alert('<b>Your browser does not support WebStorage</b>');
}
}
Loading the values:
var username, password;
username = localStorage.getItem('username');
$('#username').val(username);
$.each(heroes, function(index,value){
if( localStorage.getItem('heroActive' + index) ){
$('input#isActive' + index).attr('checked', localStorage.getItem('heroActive' + index) );
} else {
$('input#isActive' + index).removeAttr('checked');
}
$('input#level' + index).val( localStorage.getItem('heroLevel' + index) );
$('input#prestige' + index).val( localStorage.getItem('heroPrestige' + index) );
});
The list that is causing problems:
var heroes = ["Black Panther","Black Widow","Cable","Captain America","Colossus","Cyclops","Daredevil","Deadpool",/*"Doctor Strange",*/"Emma Frost",
"Gambit","Ghost Rider","Hawkeye","Hulk","Human Torch","Iron Man","Jean Grey",/*"Juggernaut",*/"Loki","Luke Cage",/*"Magneto","Moon Knight",*/"Ms Marvel",
"Nightcrawler",/*"Nova","Psylocke",*/"Punisher","Rocket Raccoon",/*"Silver Surfer",*/"Scarlet Witch","Spider-Man","Squirrel Girl",/*"Star-Lord",*/"Storm",
/*"Sue Storm",*/"Thing","Thor","Wolverine"/*,"Venom"*/];
Additionally, the last entry (Wolverine) doesn't seem to be functioning correctly. For a starter clicking the label for its activity doesn't trigger the checkbox whereas all the others do. Other problems with this entry:
Doesn't trigger my errors.js file at all
errors.js:
$.each(heroes, function(index,value){
$('input#level' + index).change(function() {
var numbers = /^[0-9]+$/;
var val = $('input#level' + index).val();
if(val > 60) {
alertify.log("Hero " + value + " cannot be above Level 60!", "", 0);
$('#level' + index).addClass('error');
} else if( isNumeric(val) ) {
if( $('#level' + index).hasClass('error') ) {
$('#level' + index).removeClass('error');
}
} else {
alertify.log("Only numbers are accepted.");
$('#level' + index).addClass('error');
}
});
});
function isNumeric(num){
return !isNaN(num);
}
Anything stored in local storage returns as a string.
So if you stored fooo = false in local storage,
if(!fooo){
bar();
}
will never execute bar();
if(fooo){
bar();
}
will always execute bar(), since a string always is true, since the variable is not empty or false.
I've banged my head against this once as well. Kinda stupid, but hey. :)
I'm changing the form action with:
window.onload = function() {
document.getElementById('pdf').onclick = addExportEvent;
document.getElementById('xls').onclick = addExportEvent;
document.getElementById('xml').onclick = addExportEvent;
document.getElementById('csv').onclick = addExportEvent;
}
function addExportEvent() {
data = grid.getAllGridData();
document.getElementById('dados').setAttribute('value', encodeURIComponent(JSON.stringify(data)));
formulario = document.getElementById('formulario'); // Line 55!
formulario.action = 'php/' + this.id + '.php';
formulario.submit();
return false;
}
But it doesn't work with Internet Explorer. It returns the following error:
Message: The object doesn't support the property or method.
Line: 55
Character: 2
Code: 0
URI: http://www.site.com/javascript/scripts.js
I think Andy E's head's comment hit it on the, well, head. You are assigning the correct element, but not declaring it using var which makes IE gag and choke and all kinds of bad stuff. Other browsers handle this just fine. So you are trying to access formulario instead of declaring it, which means it never gets the value of id: formulario
function addExportEvent() {
var data = grid.getAllGridData();
document.getElementById('dados').setAttribute('value', encodeURIComponent(JSON.stringify(data)));
var formulario = document.getElementById('formulario'); // Line 55!
formulario.action = 'php/' + this.id + '.php';
formulario.submit();
return false;
}