Renegade605 Posted March 15, 2009 Share Posted March 15, 2009 I'm trying to build a script to modify the InvisionFree forums system, but I've encountered a problem. The script won't load. I've tried linking externally (<script src="...">) and putting the script in the actual headers but it won't load. I thought it was a problem with the size of the script so I split it up into the part that should executed on page load (named 'if_dynamicavatar.js') and the functions (named 'if_dynamicavatarfunctions.js'). Now the first file executes properly, but whenever it calls for a function in the second file, the execution stops. This is the code of if_dynamicavatarfunctions.js: function testAlert(alerttxt);{ alert(alerttxt);}//For Avatar Settings Pagefunction enableRow(){ var fields = this.getElementsByTagName('input'); fields[0].onclick = function onclick(event) { disableRow.call(this.parentNode.parentNode); } for (i = 1; i < fields.length; i++) { fields[i].disabled = ""; } this.getElementsByTagName('select')[0].disabled = ""; this.getElementsByTagName('img')[0].style.display = "inline"; var tds = this.getElementsByTagName('td'); for (i = 0; i < tds.length; i++) { tds[i].className = "row4"; } tds[i-1].className = "row1";}function disableRow(){ var fields = this.getElementsByTagName('input'); fields[0].onclick = function onclick(event) { enableRow.call(this.parentNode.parentNode); } for (i = 1; i < fields.length; i++) { fields[i].disabled = "disabled"; } this.getElementsByTagName('select')[0].disabled = "disabled"; this.getElementsByTagName('img')[0].style.display = "none"; var tds = this.getElementsByTagName('td'); for (i = 0; i < tds.length; i++) { tds[i].className = "darkrow2"; }}//Get Info From Signaturefunction getDAInfo(sigcontain){ alert('Func Start'); var pos = sigcontain.innerHTML.indexOf("|DYNAMICAVATAR|"); var pos2 = sigcontain.innerHTML.indexOf("|END|",pos); if ((pos != -1) && (pos2 != -1)) { var dynamic = sigcontain.innerHTML.substring(pos+15,pos2) sigcontain.innerHTML = sigcontain.innerHTML.substring(0,pos) + sigcontain.innerHTML.substring(pos2+5); var imgs = new Array(); var imgwidth = new Array(); var imgheight = new Array(); var prob = new Array(); var starpos = 0; starpos = dynamic.indexOf("*",starpos); for (n = 0; starpos != -1; n++) { var sc1pos = dynamic.indexOf(";",starpos); prob[n] = dynamic.substring(starpos+1,sc1pos); var sc2pos = dynamic.indexOf(";",sc1pos+1); var imgsize = dynamic.substring(sc1pos+1,sc2pos); var xpos = imgsize.indexOf("x"); imgwidth[n] = imgsize.substring(0,xpos); imgheight[n] = imgsize.substring(xpos+1); starpos = dynamic.indexOf("*",starpos+1); var enduri; if (starpos == -1) { enduri = pos2; } else { enduri = starpos; } imgs[n] = dynamic.substring(sc2pos+1,enduri); } var list = new Array(); for (n = 0; n < prob.length; n++) { for (o = 0; o < prob[n]; o++) { list[list.length] = n; } } var returnar = new Array(); returnar['imgs'] = imgs[]; returnar['imgwidth'] = imgwidth[]; returnar['imgheight'] = imgheight[]; returnar['prob'] = prob[]; returnar['list'] = list[]; alert(returnar['imgs'][0]); return returnar; }} I created the testAlert() function to test if it was my function it had a problem with, but even such a simple function wouldn't execute. I've tried putting this file in the headers and in the body right before the other script.Does anyone know how to fix this?EDIT: I've done a little more digging, and it turns out the script only fails to load when the functions getDAInfo() or insertSettings() (see below) are present. function insertSettings(row,enabled,imgsrc,imgprob,imgwidth,imgheight){ var table = document.REPLIER.getElementsByTagName('table')[0]; var nr = table.insertRow((row*2)-2); var nc = nr.insertCell(0); nc.className = "darkrow1"; nc.innerHTML = " "; nc = nr.insertCell(1); nc.className = "darkrow1"; nc.innerHTML = "<b>Avatar " +row +"</b>"; nc = nr.insertCell(2); nc.className = "darkrow1"; nc.innerHTML = " "; nc = nr.insertCell(3); nc.className = "darkrow1"; nc.innerHTML = " "; nc = nr.insertCell(4); nc.className = "darkrow1"; nc.innerHTML = " "; nr = table.insertRow((row*2)-1); nc = nr.insertCell(0); nc.className = "row4"; nc.width = "32"; nc.align = "center"; nc.valign = "middle"; nc.innerHTML = "<input type=\"checkbox\" name=\"av" +row +"enabled\" onclick=\"disableRow.call(this.parentNode.parentNode);\"" +(enabled) ? " checked=\"checked\"" +" />"; nc = nr.insertCell(1); nc.className = "row4"; nc.width = "1"; nc.align = "left"; nc.valign = "middle"; nc.innerHTML = "Remote Image URI: <input type=\"text\" name=\"av" +row +"uri\" size=\"60\" value=\"" (enabled) ? imgsrc : "http://" +"\" />"; nc = nr.insertCell(2); nc.className = "row4"; nc.width = "1"; nc.align = "center"; nc.valign = "middle"; nc.innerHTML = "Probability: <select class=\"dynamic_avatar_probselect\" name=\"av" +row +"prob\" size=\"1\"><option" +(imgprob == 1) ? " selected=\"selected\"" +">1</option><option" +(imgprob == 2) ? " selected=\"selected\"" +">2</option><option" +(imgprob == 3) ? " selected=\"selected\"" +">3</option><option" +(imgprob == 4) ? " selected=\"selected\"" +">4</option><option" +(imgprob == 5) ? " selected=\"selected\"" +">5</option></select>"; nc = nr.insertCell(3); nc.className = "row4"; nc.width = "1"; nc.align = "center"; nc.valign = "middle"; nc.innerHTML = "Image Dimensions: <input type=\"text\" name=\"av1w\" size=\"4\"" +(enabled) ? (" value=\"" +imgwidth +"\"") +" /> <b>x</b> <input type=\"text\" name=\"av1h\" size=\"4\"" +(enabled) ? (" value=\"" +imgheight +"\"") +" />"; nc = nr.insertCell(4); nc.className = "row1"; nc.align = "center"; nc.valign = "middle"; nc.innerHTML = "<img border=\"0\" src=\"" +imgsrc +"\" />"; if (!enabled) { disableRow.call(nr); }} So what's wrong with these 2 functions that they would both cause the entire script not to load just by being there? Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2009 Share Posted March 16, 2009 Are you using a debugger like Firebug? If there's a syntax error in the Javascript file it won't define any of the functions. Firebug will tell you if there's a syntax error. Link to comment Share on other sites More sharing options...
Renegade605 Posted March 16, 2009 Author Share Posted March 16, 2009 Firebug will tell you if there's a syntax error.I actually can't figure out how to use the Firebug script debugger... When I enable it all I get is a window with the source code for the page on the left and an empty window on the right. Do you think you could explain how to use it? EDIT: Nevermind I figured it out. Although it's saying that exit; isn't defined. I thought that was pretty much universal. How do you exit execution of something in JS if not the exit; function? It also says that this.getElementsByTagName is not a function, why is that not a function?Anyway I figured out what was wrong with the insertSettings() function (apparently the "(expression) ? <return when true> : <return when false>" shortcut only works in PHP. I could've sworn I saw it in another JS but maybe not.). Also, I scrapped the getDAInfo() function because I ended up not needing it. So that problem is solved. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 16, 2009 Share Posted March 16, 2009 Although it's saying that exit; isn't defined. I thought that was pretty much universal. How do you exit execution of something in JS if not the exit; function?If it's a function you want to escape from, then use a return statement. If it's a control structure like a loop, you can use a break statement. Javascript doesn't have a way to stop all Javascript code from running, each Javascript thread can only control itself.It also says that this.getElementsByTagName is not a function, why is that not a function?That depends what this is set to. You can write that to the Firebug console as well to check it using console.log(this).apparently the "(expression) ? <return when true> : <return when false>" shortcut only works in PHPJavascript does define the ternary operator as a shortcut if/else structure, this will work:var str = (window == window.parent ? 'top level' : 'not top');alert(str); Link to comment Share on other sites More sharing options...
Renegade605 Posted March 17, 2009 Author Share Posted March 17, 2009 If it's a function you want to escape from, then use a return statement.So just return any value to stop execution?That depends what this is set to. You can write that to the Firebug console as well to check it using console.log(this). this is set to a <tr> element. The code still works so I don't see how it can't be a function.Javascript does define the ternary operator as a shortcut if/else structure, this will work:var str = (window == window.parent ? 'top level' : 'not top');alert(str);So is it just because I placed the ) in the wrong place?(execute) ? "checked=\"checked\""Also, Firebug is giving me this error:uncaught exception: Node was not found (NS_ERROR_DOM_NOT_FOUND_ERR)http://site.theunitedfrontclan.com/if_dynamicavatar.jsLine 26{ document.removeChild(newimg[rownum]); }How can I have it check if the node exists before trying to remove it. I tried adding "if (newimg[rownum])" but that didn't do it. Link to comment Share on other sites More sharing options...
jesh Posted March 17, 2009 Share Posted March 17, 2009 So just return any value to stop execution?You can just use "return". You don't have to actually return a value if you aren't going to use a value.function something(){ if(somethingelse == true) { return; } else { // do something. }} So is it just because I placed the ) in the wrong place?(execute) ? "checked=\"checked\""As far as I can remember, you have to have all three arguments to the ternary operation:booleanTest ? whatToDoIfTrue : whatToDoIfFalse; So, in your case: (execute) ? "checked=\"checked\"" : ""; Link to comment Share on other sites More sharing options...
justsomeguy Posted March 17, 2009 Share Posted March 17, 2009 How can I have it check if the node exists before trying to remove it. I tried adding "if (newimg[rownum])" but that didn't do it.If I want to check if something exists I use the typeof operator.if ((typeof el) != "undefined")Be careful with parentheses there. That's not the same as this:if (typeof el != "undefined")In your case, is newimg[rownum] an actual node that is a child of the node you're trying to remove it from? It can't just be an ID or something, it has to be a node object. Link to comment Share on other sites More sharing options...
Renegade605 Posted March 17, 2009 Author Share Posted March 17, 2009 If I want to check if something exists I use the typeof operator.if ((typeof el) != "undefined")Be careful with parentheses there. That's not the same as this:if (typeof el != "undefined")In your case, is newimg[rownum] an actual node that is a child of the node you're trying to remove it from? It can't just be an ID or something, it has to be a node object.I tried this:var oldimg = this.getElementsByTagName('img')[0];console.log(oldimg);if ((typeof oldimg) != "undefined") { document.removeChild(oldimg); } When the img does not exist, 'undefined' is logged to the console and nothing happens. But when it does exist the proper object is logged to the console and I get an error that the node can't be found, and execution stops. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 17, 2009 Share Posted March 17, 2009 That means the parent is wrong, or that oldimg doesn't exist as a direct child under document. Maybe use this:oldimg.parentNode.removeChild(oldimg); Link to comment Share on other sites More sharing options...
Renegade605 Posted March 17, 2009 Author Share Posted March 17, 2009 That's done it; I didn't realize it had to be a direct child for that function to work. Thanks. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.