help using external javascript in div

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ktrw25
    New Member
    • Feb 2007
    • 17

    help using external javascript in div

    Hello, I'm using the Yahoo UI to create a Panel in a custom application. I use ajax to request html from a php page to create the panel. The php page echo's out the entire html page including the src for javascript (which is an external file) for the html page. The returned html is then made into a div with a z-index of 6 to make the panel. This panel has buttons that use functions from the javascript that was passed back from the php page. The buttons/javascript work fine in Firefox, but not at all in IE.

    Any thoughts?
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Could you show some code or a test page.

    Comment

    • naurus
      New Member
      • Jun 2007
      • 21

      #3
      could it be that you're using document.getEle mentById() in your scripts? If you are, IE 6 doesn't support all the web standards, getElementById being one of them.

      If that's the case, using this function could help:
      Code:
      function fetchById(id){
      	if (document.getElementById){
      		var fetch = document.getElementById(id);
      	}
      	else if (document.layers){
      		var fetch = document.layers[id];
      	}
      	else if (document.all){
      		var fetch = document.all[id];
      	}
      	return fetch;
      }
      Just do a search in your document for "document.getEl ementById(" and use the replace field to replace it with "fetchById( ".

      Hope that helps,
      Naurus

      Comment

      • ktrw25
        New Member
        • Feb 2007
        • 17

        #4
        A button on the main page calls Panel() which gets the html from PANEL.php This is then displayed in a div on the main page. all the html shows up fine, but the javasript does not work in ie. Works fine in FF.

        //**********funct ion that gets the html that we need ****
        [CODE=javascript]function Panel () {

        var returnajax = function(o){
        //returned response is html
        var html = o.responseText;
        YAHOO.namespace ("example.conta iner");

        YAHOO.callhome. container.panel = new YAHOO.widget.Pa nel("panel");

        YAHOO.example.c ontainer.panel. setBody(html);
        YAHOO.example.c ontainer.panel. render(document .body);

        }
        //handle the return
        var handle_return =
        {
        success:returna jax
        };

        var url = "PANEL.php" ;

        //get the html from PANEL.php
        var get_callhome = YAHOO.util.Conn ect.asyncReques t('GET', callhomeurl, handle_return);
        }
        [/CODE]*************** ******PANEL.php**********
        [PHP]// php page used for returning the html
        echo <<< EOT;
        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
        <html>
        <head>
        <meta http-equiv="content-type" content="text/html; charset=windows-1250">
        <meta name="generator " content="PSPad editor, www.pspad.com">
        <title></title>
        <script type="text/javascript" src="applicatio n/scripts/testscript.js"> </script>
        <link rel="stylesheet " href="password. css" type = "text/css">
        </head>

        <body>

        <div id="buttons">
        <center>
        <button class="inputbut ton" type="button"id ="button1" onclick="test() ">New User</button>
        </center>
        </div>

        </body>
        </html>
        EOT;

        [/PHP]**************application/scripts/testscript.js*******
        [CODE=javascript]//function in testscript.js
        function test() {
        alert("hello");
        }
        [/CODE]*************** *************** *************** *************** *****
        Last edited by acoder; Aug 2 '07, 09:47 AM. Reason: Added code tags

        Comment

        • ktrw25
          New Member
          • Feb 2007
          • 17

          #5
          IE acts like it is not loading the external script

          Comment

          • drhowarddrfine
            Recognized Expert Expert
            • Sep 2006
            • 7434

            #6
            Change this:
            Code:
            <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
            <html>
            <head>
            <meta http-equiv="content-type" content="text/html; charset=windows-1250">
            To this:
            Code:
            <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
               "http://www.w3.org/TR/html4/loose.dtd">
            <html>
            <head>
            <meta http-equiv="content-type" content="text/html; charset=utf-8">
            This gets IE out of 'quirks mode' and brings your page into the 21st century. And it gave me something to do. None of that has anything to do with fixing the problem.

            The <center> tag has been deprecated for years. You should use CSS. Of course, you should use a strict doctype, too, but you can't if you use <center>.

            Comment

            • ktrw25
              New Member
              • Feb 2007
              • 17

              #7
              Replacing that code didn't seem to help matters. Can IE not use an external script from a dynamically created div or am I on the wrong track?

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                Originally posted by ktrw25
                Replacing that code didn't seem to help matters. Can IE not use an external script from a dynamically created div or am I on the wrong track?
                Does the stylesheet work?

                Comment

                • ktrw25
                  New Member
                  • Feb 2007
                  • 17

                  #9
                  No the stylesheet does not work either.

                  Comment

                  • acoder
                    Recognized Expert MVP
                    • Nov 2006
                    • 16032

                    #10
                    Originally posted by ktrw25
                    No the stylesheet does not work either.
                    Then you need to dynamically add the script and the stylesheet to the page using code similar to the following:
                    [CODE=javascript]var scrpt = document.create Element("script ");
                    scrpt.src = theurl;
                    scrpt.type="tex t/javascript";
                    document.getEle mentsByTagName( "head")[0].appendChild(sc rpt); [/CODE]and something similar for the CSS stylesheet. Note that this will only work in modern browsers.

                    Comment

                    • ktrw25
                      New Member
                      • Feb 2007
                      • 17

                      #11
                      Awesome, works great. That's exactly what I wanted it to do.

                      Comment

                      • acoder
                        Recognized Expert MVP
                        • Nov 2006
                        • 16032

                        #12
                        Originally posted by ktrw25
                        Awesome, works great. That's exactly what I wanted it to do.
                        Glad you got it working. Post back anytime you have any more problems.

                        Comment

                        Working...