Forum Moderators: open

Message Too Old, No Replies

Dynamic insertion of .js files, but cant remove them.

         

iLLin

10:59 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



I have a form that depending on what boxes you choose, will load a certain .js file for error checking. Now you press the checkbox, it loads the file but when you uncheck it, I can't get it to be removed. The removeChild() doesn't seem to work? I can add text and remove text just fine, but I can't get the javascript removed. Here is my code.


var test = document.createTextNode("this is a test");

var eval_js = document.createElement('script');
eval_js.setAttribute('language', 'javascript');
eval_js.setAttribute('type', 'text/javascript');
eval_js.setAttribute('src', 'javascript/eval.js');

function include() {
var html_doc = document.getElementById ('javascript');

html_doc.appendChild(eval_js);
html_doc.appendChild(test);

return false;
}

function remove() {
var html_doc = document.getElementById ('javascript');

html_doc.removeChild(eval_js);
html_doc.removeChild(test);

return false;
}

I check the box and it loads the javascript checks fine and I get my alerts for what I need to fill out. If I uncheck it, I cant get the alerts to go away, they are still active.

Thanks,
Dennis

ajkimoto

11:55 pm on Nov 4, 2005 (gmt 0)

10+ Year Member



iLLin,

I don't think that I have ever tried to remove a reference to an external js file before. Now, having tried it using a 'swapper' function, I find that the functions persist even if you remove the reference to the js file.

One possible solution would be to null out each function that was contained in the js file that you want to remove. I have tried this in FF/Moz as well as IE6 and it seems to work.

If you have a function in js1.js called 'myfunc1' you can remove it by removing the reference to the external file and then running the following code:

myfunc1=null

Maybe not quite what you are looking for, but perhaps it will do for now...

ajkimoto

Bernard Marx

8:36 pm on Nov 5, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's true. A script file simply dumps all its data into the scripting environment. The data no longer has any specific relationship with a particular script file.

Why do you need to remove the data. Can't you simply ignore it?

iLLin

10:14 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



OK here is the scenerio on whats happening. At the top of my form I have two checkboxes, Home improvement and Home evaluation. Now depending on which checkbox they select I have hidden fields that display when you click the box, and there is different fields per checkbox. But they can select both checkboxes if they want. Now I have all this working perfect but I want to be able to load error checking for each checkbox independently. I have server side error checking too with php for non javascript users but javascript always makes things nicer. Anyway, If they select the home eval box then 15 more fields display they need to fill out for that option and the error checking script loads, now if they uncheck that box and decide to do just Home improvement, then the error checking is still in effect even though those form fields disappeared. Does this make more sense?

My error checking is done through a class and a variable so I'm going to try to clear that variable out and see if that works when the box is unchecked. I'll let you know.

Thanks,
Dennis

iLLin

10:40 pm on Nov 5, 2005 (gmt 0)

10+ Year Member



That worked guys thank you for your help. I setup two classes, one for each checkbox. Then addValidation on check, clearValidation on uncheck. Works like a charm :)

-Dennis