Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer script tag adding internal code with DOM ie problems

    Hi everyone, i have a problem with the script tag and with internet explorer.
    Just remark here that ALWAYS problems start and finish with internet explorer, as mozilla (firefox 0.8) works always fine.

    My code:

    var counter_scr = document.createElement('script');
    counter_scr.setAttribute('type','text/javascript');
    counter_scr.appendChild(document.createTextNode('counter(document.forms.bform_articlesForm.body,8000 );'));
    counter_scr.appendChild(document.createTextNode('see_counter();'));
    articles_output.appendChild(counter_scr);


    where articles_output is a div.

    It does work in mozilla, but not in ie (6.0).
    The problem is the counter_scr.appendChild lines.

    Any suggestion?? Thanks!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,037
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Try replacing

    counter_scr.setAttribute('type','text/javascript');

    with:

    counter_scr.type = 'text/javascript';

  • #3
    New to the CF scene
    Join Date
    May 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    still not :((

    var counter_scr = document.createElement('script');
    alert('hola')
    counter_scr.type = 'text/javascript';
    alert('hola2')
    counter_scr.appendChild(document.createTextNode('counter(document.forms.bform_articlesForm.body,8000 );'));
    counter_scr.appendChild(document.createTextNode('see_counter();'));
    alert('hola3')
    articles_output.appendChild(counter_scr);

    ie6.0, hola and hola2 appears, hola3 do not.
    any idea?

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The problem lies in appendChild. Ie6w gives me an "invalid pointer" message when I try to use it on a script element. However, I tested using the following, which seems to work in moz, op7.50, ie5w, ie5.5w and ie6w at least.
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
    <HEAD>
    <TITLE>Various browsers testcase: Parsing of dynamically created embedded scripts</TITLE>
    <SCRIPT TYPE="text/javascript">
    window.onload=function(){
        var script=document.createElement('script');
        script.setAttribute('type','text/javascript');
        /*script.appendChild(
        document.createTextNode(*/
        script.text='alert(\'Dynamically created embedded script has been parsed\');'
        /*));*/
        document.getElementsByTagName('head').item(0).appendChild(script);
        
        alert('Embedded script test:\n    '+
            'SCRIPT element: '+script+'\n    '+
            'TYPE attribute: '+script.getAttribute('type')+'\n    '+
            'parent element: '+script.parentNode+'\n    '+
            'outerHTML property: '+(script.outerHTML||(new XMLSerializer).serializeToString(script))+'\n    '+
            'innerHTML property: '+script.innerHTML+'\n    '+
            'text property: '+script.text+'\n    '+
            (script.firstChild?
                ('normalised textual contents: '+(script.normalize(),script.firstChild.data)):
                ''));
    };
    </SCRIPT>
    </HEAD>
    <H1>Various browsers testcase: Parsing of dynamically created embedded scripts</H1>
    <P>There should technically be no difference between if HTML and XML is created dynamically through the DOM or included in the original page source.
    <P>There should be two alerts from this page. One "Dynamically created embedded script has been parsed" and one listing something like this:
    <PRE><CODE>Embedded Script test:
        SCRIPT element: [object HTMLScriptElement]
        TYPE attribute: text/javascript (specified)
        parent element: [object HTMLHeadElement]
        outerHTML property: &lt;SCRIPT TYPE="text/javascript"&gt;alert('Dynamically created embedded script has been parsed');&lt;/SCRIPT;&gt;
        innerHTML property: alert('Dynamically created embedded script has been parsed');
        text property: alert('Dynamically created embedded script has been parsed');
        normalised textual contents: alert('Dynamically created embedded script has been parsed');</CODE></PRE>
    <P>David "liorean" Andersson, 2004
    So, instead of placing Text nodes within the script elements using appendChild, change it's text property to the appropriate text.
    Last edited by liorean; 06-01-2004 at 03:53 PM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #5
    New to the CF scene
    Join Date
    May 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks!

    It worked, sadly in my case it doesn't as my script needs to call a function giving as an argument a reference of a textarea that just few lines above has been introduced.

    Mozilla (again) does the job, but ie not, i've heard that it is asyncronous and then the reference i pass to the function cannot be read because ie has not still put inside.

    And i cannot use the window.onload because all this stuff runs inside a function.

    So, first thing i have done is to call the funcion directly instead of adding the script tag inside (i really didn't have to!!).
    Next has been to write the function called (in an outside .js file) to the same .js file, and then to use directly the reference returned when created the node.

    You could ask why not send that reference to the function, but message is the same "reference is null".

    So summarizing, my initial question (about the script tag) has been solved, so thanks thousands.

    Then the other problem lies on ... hell, i'm just working on it!!

  • #6
    New to the CF scene
    Join Date
    May 2004
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks 2 !

    Finally, the last problem (of mine) was false ... so my solution has been eliminating the tag script i used to call a function and instead to call the function directly.

    Anyway i've tested your solution and it has worked so for a next time i need that functionality i (and everyone that search google) knows it.
    So thanks a lot!!

  • #7
    New to the CF scene
    Join Date
    Jul 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I had the same problem and looking for the solution in google this is the first link I have found. Though I see the date of the last post is two years ago, I write the solution I found to save it in Internet.

    Internet Explorer doesn't allow to add child nodes to scripts tags, for that reason it doesn't work "appendChild", "innerHTML", etc. The solution is to use "text" property. It works in Internet Explorer and Firefox:

    var head = document.getElementsByTagName("head");
    var script = document.createElement("script");
    head[0].appendChild(script);
    script.type = "text/javascript";
    script.text = "alert('hello world');";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •