I keep getting an error with roughly the following code.
38. var element_to_precede = document.getElementById("element");
39. var parent_element = element_to_precede.parentNode;
It equates roughly to
line 39: TypeError: element_to_precede is null"
I have been reading around but what I have tried just doesn't work. I have in my main.js,
'contentScriptWhen: "ready"'
in theory this is supposed to load the script only when everything else has finished loading. The only logical explanation is that this is failing somehow.
I have tried wrapping the code with
window.onload = function() {
//Summon function that calls for element_to_precede here
}
Where I ask it to load the elements. Unfortunately it doesn't seem to change anything.
I'm primarily messing with trying to create an extension so I'm using mozilla's tutorials.
Please don't suggest jQuery I'm sure there's a solution in standard JavaScript that I'm missing or not implementing correctly. I'm likely missing something fundamental but for the life of me can't figure out what it is.
Edit.
For example, the code I've tried.
window.onload=function(){
var container = document.createElement("div");
var element_to_precede = document.getElementById("element")
var parent_element = element_to_precede.parentNode;
parent_element.insertBefore(container, element_to_precede);
}
Returns
JavaScript error: line 39: TypeError: element_to_precede is null
Edit
After going through the code again I have found that contrary to what I'm being told the system does...something isn't happening
After asking it the following commands
if(document.readyState === "complete") {
console.log("In theory everything is already loaded");
console.log("Detected: " + document.getElementById("element") + " element");
}
I discovered that it would cycle round four times. Each time it would tell me that everything has already been loaded but that null element was detected. Implying that in fact the page is not "complete".
I'm tempted to just put in a wait order for 200 milliseconds or so every time it finds out that it wasn't detected.
So back to the original question. How the heck do I stop this?
Related
So, I'm having some trouble returning a value from an ExternalInterface call. I have a piece of code that looks like this:
var a:String = String(ExternalInterface.call("function() { var returnTest = 'test'; alert(returnTest); return returnTest;}"));
ExternalInterface.call("alert", a);
The first alert (in the anonymous function on line 1) is correct (obviously). However, the alert on line 2 is returning null 90% of the time in IE10. It works everytime in Firefox though.
To further explain the bit about working 90% of the time, it seems I can roll the dice again on whether or not it will work by adding or removing seemingly meaningless alerts. For example: let's say it's not working, I could add an alert and it will start working. Or, say it is working, I could add an alert for debugging, and it stops working, remove the alert, still doesn't work, add the alert back, and it starts working again. I know this isn't what's happening, but it's behaving as if a coin is flipped every time an alert is added or removed.
And this all only happens in IE, works perfectly every time in Firefox.
Thanks.
Edit:
The code I provided isn't the actual code that needs to work, but rather the code that I wrote to verify where the problem was. The actual situation is that there's a JavaScript property in the environment our Flash is running in that we need to know, but we don't have access to the HTML or JavaScript the SWF will be running in. The actual code I need to run looks more like this:
var pageNameFromJS:String = String(ExternalInterface.call("function() { var pageName = ServerObject.currentPage.name; alert(pageName); return pageName;}"));
ExternalInterface.call("alert", pageNameFromJS);
The alert in the first line is just to make sure that ServerObject.currentPage.name works, which it does. The alert in the second line is debug code that was added when we noticed that functions that require pageNameFromJS weren't working.
Really I dont know why you complicate things like this ;)
You can do it easier :
AS3 code:
ExternalInterface.addCallback("flash_function", flash_function);
function flash_function(from_js_param){
trace('param received from js : '+from_js_param)
}
ExternalInterface.call("js_function", to_js_param)
JS code:
function js_function(from_flash_param){
var to_flash_param = 'I received your '+from_flash_param;
(get_your_swf).flash_function(to_flash_param);
}
I have this code that gives me an "typerror" when it's called... could somebody please give me an idea what is wrong, I can't seem to understand what is wrong with it..
document.getElementById('cblberrormsg'+curID).style.display = "block";
var res = ''+ response;
document.getElementById('cblberrormsg'+curID).innerHTML = res;
Thank you.
document.getElementById('cblberrormsg'+curID) is returning null. That means that the object with that id does not exist.
The most common reason for this is because you're trying to execute this line of code too early before that part of the page has been parsed and loaded. Your code must either be in a <script> tag that is AFTER the relevant HTML or you must use a function that waits to execute your script until the page is loaded such as this.
But, since in this case, you're constructing an id string using a variable, it also could be that curID isn't what you think it is or has gone beyond the acceptable values in your page.
Once you've made absolutely sure that this code is not being executed until AFTER the page HTML has been loaded, then I'd suggest you instrument it like this:
console.log("curID=" + curID);
console.log("document.getElementById('cblberrormsg'+curID)=" + document.getElementById('cblberrormsg'+curID);
document.getElementById('cblberrormsg'+curID).style.display = "block";
var res = ''+ response;
document.getElementById('cblberrormsg'+curID).innerHTML = res;
Then, look in your debug log when the error occurs and see what the value is for curID when the error occurs.
jfriend00 is right, the object you're referring to doesn't exist.
If the document isn't loaded yet OR for some reason you are not properly setting curID, the code you've got will go boom every time.
You can test for this by changing your code up a bit:
var foo = document.getElementById('cblberrormsg'+curID);
if(foo){
foo.style.display = "block";
// foo exists
// do whatever, yadayadayada
}
I have been trying to figure out this particular problem in my developer tools, but I've had no luck thus far. I have an error on one of my js files that says
Uncaught TypeError: Cannot read property 'value' of null
The following error refers to the 1st variable of dt_version below. The particular thing is if I comment out the first line of code. I get the same error on the following variables of offload1 and offload2. The variable is a number that I am trying to get passed over. I run this function on my body when the page loads...onload=updatetotal();
function updatetotal() {
var dt_version = document.getElementById("dt_version").value-0;
var offload1 = document.getElementById("capacity_offload1").value-0;
var offload2 = document.getElementById("capacity_offload2").value-0;
var offload3 = document.getElementById("capacity_offload3").value-0;
}
If a run an if statement looking for document.getElementByID("dt_version");...it defaults to false..so its not being carried over though on the previous page, I can see its input fine with the value in it. What am I missing here guys?
This error means that the id dt_version does not exist. Check your html to make sure it is there:
var dt = document.getElementById("dt_version");
if (dt){
// do your stuff
}else {
console.log("dt does not exist")
}
Another cause for this error may be- as you are calling the javascript function on page load there is a possible chance that your control is not yet completely rendered to the page. A simple solution is just move that control to the beginning of the page. If it doesn't work then an reliable solution is, call the function inside jquery $(document).ready().
We see this occasionally in web apps on Firefox. What triggers it, and how do we prevent it? It seems to happen sporadically and the error message yields no useful information about line locations.
A quick google search yielded this:
http://blowery.org/2008/02/28/object-cannot-be-created-in-this-context-code-9/
...check your code to see if you’re
trying to grab a reference to the
computed style on a null reference.
It appears to be connected with the Dojo framework.
Edit: Ha. Sorry I gave you your own blog as an answer. I guess I don't completely understand what you're asking for. If you want to avoid the error, you could use object checking before running the applicable code.
function isValidObject(someObject)
{
return typeof someObject != null;
}
var obj1 = "Hello World";
if(isValidObject(obj1))
{
//This code will run
}
if(isValidObject(ob2))
{
//This code will not run
}
Hope that's helpful.
I am using a javascript called 'Facelift 1.2' in one of my websites and while the script works in Safari 3, 4b and Opera, OmniWeb and Firefox it does not in any IE version.
But even in the working browser i get the following error I cannot decipher.
Maybe in due time—with more experience in things Javascript—I will be able to but for now I thought I would ask some of you, here at SO.
The following is the error popup i get in IETester testing the page for Interet Explorer 6,7 and 8:
The following is from the Firebug console in Firefox 3.0.6:
The website is: http://www.457cc.co.nz/index.php In case it helps you see the problem mentioned in action.
I have also looked up what line 620 corresponds to which is:
"line 76" is:
this.isCraptastic = (typeof document.body.style.maxHeight=='undefined');
which is part of this block of code (taken from the flir.js):
// either (options Object, fstyle FLIRStyle Object) or (fstyle FLIRStyle Object)
,init: function(options, fstyle) { // or options for flir style
if(this.isFStyle(options)) { // (fstyle FLIRStyle Object)
this.defaultStyle = options;
}else { // [options Object, fstyle FLIRStyle Object]
if(typeof options != 'undefined')
this.loadOptions(options);
if(typeof fstyle == 'undefined') {
this.defaultStyle = new FLIRStyle();
}else {
if(this.isFStyle(fstyle))
this.defaultStyle = fstyle;
else
this.defaultStyle = new FLIRStyle(fstyle);
}
}
this.calcDPI();
if(this.options.findEmbededFonts)
this.discoverEmbededFonts();
this.isIE = (navigator.userAgent.toLowerCase().indexOf('msie')>-1 && navigator.userAgent.toLowerCase().indexOf('opera')<0);
this.isCraptastic = (typeof document.body.style.maxHeight=='undefined');
if(this.isIE) {
this.flirIERepObj = [];
this.flirIEHovEls = [];
this.flirIEHovStyles = [];
}
}
The whole script is also available on my server: http://www.457cc.co.nz/facelift-1.2/flir.js
I just don't know where to start looking for the error, especially since it only affects IE but works in the rest. Maybe you guys have an idea. I would love to hear them.
Thanks for reading.
Jannis
PS: This is what Opera's error console reports:
JavaScript - http://www.457cc.co.nz/index.php
Inline script thread
Error:
name: TypeError
message: Statement on line 620: Cannot convert undefined or null to Object
Backtrace:
Line 620 of linked script http://www.457cc.co.nz/facelift-1.2/flir.js
document.body.appendChild(test);
Line 70 of linked script http://www.457cc.co.nz/facelift-1.2/flir.js
this.calcDPI();
Line 2 of inline#1 script in http://www.457cc.co.nz/index.php
FLIR.init();
stacktrace: n/a; see 'opera:config#UserPrefs|Exceptions Have Stacktrace'
I agree with tvanfosson - the reason you're getting that error is quite likely because you're calling init() before the page is done loading, so document.body is not yet defined.
In the page you linked, you should move the following code to the bottom of the page (just before the closing html tag:
<script type="text/javascript">
FLIR.init({ path: 'http://www.457cc.co.nz/facelift-1.2/' });
FLIR.auto();
</script>
Even better, you should attach the initialization to the document's ready event. If you do it this way, there is no need to even move your javascript to the bottom of the file. Using jquery:
$(document).ready( function(){
FLIR.init({ path: 'http://www.457cc.co.nz/facelift-1.2/' });
FLIR.auto();
});
More on jquery's document.ready event »
Edit Answer left for context. See #Triptych's (accepted) answer for the correct resolution.
My suggestion is to move the inclusion of the javascript to the end of your mark up. I think what is happening is that the code is executing before the DOM is completely loaded and thus the document.body is null when you try to reference it in determining the maxHeight style property. Moving the inclusion of the javascript to the end of your markup should be enough to guarantee that the body of the document is loaded at least and avoid this particular error.
... rest of html....
<script type='text/javascript'
src='http://www.457cc.co.nz/facelift/flir.js'>
</script>
</body>
</html>
Install .net Framework v2 and solve the problem.