JS function in generated html script won't run contents - javascript

I have this function
function runcode(code) {
var tag = document.createElement("script");
document.body.appendChild(tag);
tag.innerHTML = code;
}
And when I call this
certainValue = "test";
anotherValue = "alert('hi');";
runcode("function " + certainValue + "() {" + anotherValue + "}");
And then call this in the Chrome J's console
test
It tells me the function code. But when I call
test();
nothing happens. Why?

Related

Print Options Do Not Open

I have an html file in google script with JS, trying to print using the following code. It opens the document without the print options, I still have to do the cmd+P to print. Any idea, please?
function printBadge() {
var attendee = document.getElementById("info").value;
var fullName = "xxx";
var snb = "xxx";
var printFonts = "fonts";
var printStyle = "style";
var printArea = "badge info";
w = window.open();
w.document.write(printFonts + printStyle + printArea).html();
w.document.close();
w.focus();
w.print();
w.close();
return false;
}
The reason is this line w.document.write(printFonts + printStyle + printArea).html();. There is no function .html() associated to document.write and when you try to execute it, it fails there and so window gets stuck.
It needs to be w.document.write(printFonts + printStyle + printArea) without .html() function.
Hope it helps. Revert for any doubts.

script loaded asynchronously using appendChild gets loaded last

I created a script that add javascript files by appending it to the head tag.
Using the code below that I created:
var Dim = (function(s){
var storage;
function loadError (oError) {
throw new URIError("The script " + oError.target.src + " is not accessible.");
}
return {
require : function(script,callback){
var oScript = document.createElement("script");
oScript.type = "text\/javascript";
oScript.onerror = loadError;
if (callback) {
oScript.onload = callback;
}
document.head.appendChild(oScript);
oScript.src = script;
}
};
})();
When we use the script like this:
Dim.require("script.js", function(){alert(token)}); alert(token);
and script.js code is:
alert(" Ok"); var token = " Hello";
The alert(token) throws an error.
Can anyone point me the Problem?
[Question was edited]
Dim.require("script.js", function(){alert(token)}); alert(token);
It runs first alert because its in callback which is called when the script is loaded by the browser.
if (callback) {
oScript.onload = callback;
}
And second is not giving you an alert because it gives you an error
Uncaught ReferenceError: token is not defined
You are loading your script.js asynchronously and while executing second alert , the script.js not loaded yet.

javascript event listener for syntax and internal errors

I am working on a function to track anytime there is a js error on a page. I have 4/6 error types working but can't seem to figure out a function for syntax errors, or do they just stop script execution to the point it doesn't function? Also, I am not sure how to test for internal errors? Edit: InternalError is only in firefox, but need a test for EvalError, which I will post separately. Another Edit: it appears EvalError is legacy and not fully supported by modern browsers.
No libraries such as jQuery can be used, only native js.
Here is the code with the answer added:
errorTracking = function errorCaught( ev ) {
document.getElementById('error').innerHTML = '';
var errFile = '';
var errLine = '';
if(ev.filename) { errFile = ev.filename; }
if(ev.lineno) { errLine = ev.lineno; }
var errStr = 'ERROR: ' + ev.error + ', LOCATION: ' + errFile + ', LINE NUMBER:' + errLine;
document.getElementById('error').innerHTML = '<strong>Message:</strong> ' + ev.error + '<br /><strong>Location:</strong> ' + errFile + '<br /><strong>Line Number:</strong> ' + errLine;
// Omniture Error Tracking.
//_satellite.setVar('jsError', errStr);
ev.preventDefault();
};
if(window.addEventListener) {
window.addEventListener( "error", errorTracking, false );
document.getElementById('errRef').addEventListener('click', function() {
var refErr = asdf.asdf.length;
});
document.getElementById('errTyp').addEventListener('click', function() {
var typeErrVar = null;
var typeErr = typeErrVar();
});
document.getElementById('errRan').addEventListener('click', function() {
Array.apply(null, new Array(1000000)).map(Math.random);
});
document.getElementById('errURI').addEventListener('click', function() {
decodeURIComponent("%");
});
document.getElementById('errSyn').addEventListener('click', function() {
var script = document.createElement('script');
script.text = document.getElementById('error');
document.getElementsByTagName('head')[0].appendChild(script);
});
document.getElementById('errEva').addEventListener('click', function() {
});
}
http://jsfiddle.net/swv55c35/17/
<script/>'s will be parsed completely, when a script contains a syntax-error, the entire script will be discarded. Create the syntax-error outside of the script that handles the errors, and it will work.
http://jsfiddle.net/swv55c35/3/

Calling multiple functions from Salesforce Javascript Button

I'm fairly new at javascript, so this might be a silly question.
I've built a javascript button in Salesforce that does the following when clicked (http://i.stack.imgur.com/hqMhp.png)
What I'd like it to do for now is just display 4 separate alerts (I'll add in the real functions later).
Here's the code I'm using:
{!REQUIRESCRIPT("/soap/ajax/20.0/connection.js")}
var leadObj = new sforce.SObject("Lead");
var countryVal = "{!Lead.Country }";
var leadID = "{!Lead.Id}";
var ownerID = "{!Lead.OwnerId }";
function insertScript(script){
var targetNode = document.createElement('div'); // construct div for script injection
document.body.appendChild(targetNode);
try {
var el = document.createElement('script');
el.type="text/javascript";
el.innerHTML = script;
targetNode.appendChild(el);
} catch (e){
var el = document.createElement('span');
targetNode.appendChild(el);
el.innerHTML = "<br /><scr"+"ipt type='text/javascript' defer='defer'>"+script+"</script" + ">";
}
var box = new SimpleDialog("hersh"+Math.random(), true);
parent.box = box;
box.setTitle("Lead Rerouter");
box.createDialog();
box.setWidth(350);
box.setContentInnerHTML("<p align='center'><img src='/img/icon/profile24.png' style='margin:0 5px;'/><img src='/img/sales/quotes/sync_overlay_arrow.png' style='margin:5px;'/><img src='/img/icon/custom51_100/globe24.png' style='margin:0 5px;'/></p><p align='center'>Which region should this lead be routed to?</p><p align='center'><br /><button class='btn' onclick='routeAPAC(); return false;'>APAC</button><button class='btn' onclick='routeEMEA(); return false;'>EMEA</button><button class='btn' onclick='routeNA(); return false;'>NA</button><button class='btn' onclick='routeLATAM(); return false;'>LATAM</button><br><button class='btn' onclick='window.parent.box.hide(); return false;'>Cancel</button></p>");
box.setupDefaultButtons();
box.show();
}
script = "function routeAPAC(){alert (\"Lead routed to APAC!\")}";
script = "function routeEMEA(){alert (\"Lead routed to EMEA!\")}";
script = "function routeNA(){alert (\"Lead routed to NA!\")}";
script = "function routeLATAM(){alert (\"Lead routed to LATAM!\")}";
insertScript(script);
What am I doing wrong here?
Thanks!
You are over-complicating it.
document.querySelector(".generatorbutton").onclick = function() {
//add the window
}
var actionlist = {
".apa": function(){
//things to do onclick
},
".emea": function(){
//things to do onclick
},
".na": function(){
//things to do onclick
},
".latam": function(){
//things to do onclick
},
};
for(selector in actionlist) {
document.querySelector(selector).onclick = actionlist[selector];
}
Take a look at this for future references.
Hope this helps!
Due to overwriting of script variable, In your above code you can have only last one function routeLATAM will define through insertScript function
If you concatenate like below you will see all function definitions available
script = "function routeAPAC(){alert (\"Lead routed to APAC!\")}";
script += "function routeEMEA(){alert (\"Lead routed to EMEA!\")}";
script += "function routeNA(){alert (\"Lead routed to NA!\")}";
script += "function routeLATAM(){alert (\"Lead routed to LATAM!\")}";
insertScript(script);

Internet Explorer 'null or not an object' error - code works ok in Firefox and Safari

Error: 'document.forms.0.Docunid_5.value is null or not an object
From function in JS Header in Lotus Notes form:
function saveWebViewDSFF(docnumber)
{
var docID = eval("document.forms[0].Docunid_" + docnumber + ".value");
var Comment9 = eval("document.forms[0].Comment9_" + docnumber + ".value");
var agentPath = "//www.mananua.com/foliw/ekupulibrary.nsf/(SaveWebViewDSFF)?openagent&unid=" + docID + "& Comment9=" + Comment9;
document.getElementById("agentOutput").src = agentPath;
}
When are you calling this function? If you are calling it before the form has a chance to load then move the call to the bottom of the HTML or use the window.onload function.
Example
</body>
<script type="text/javascript">
window.onload = function () { saveWebViewDSFF(5); };
</script>
</html>

Categories

Resources