SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 33 of 33
  1. #26
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    7stud,

    Thanks very much! Safari was eactly what I was unsure of. So I guess we just need to use innerHTML there.

    In IE the .text = .text technique works, but appending a text node does not.

    FF supports the .text property because it is standard on script elements.


    Jim,

    This technique does not parse the Js - altho it does get it as a string. The script element is a valid node in the doc - but its script just wasn't executed.

    You have a very good point about the scoping pitfalls - but there are gotchas no matter what technique you use. Some people really like the fact that this technique executes the script at "global" scope (and doesn't use the 'evil' eval ) - but they must design their Js accordingly. Some people prefer to use "eval" because it executes the script at "local" scope (among other reasons) - and again, they must design their Js accordingly (functions will not be available at global scope [as an aside, setTimeout is a handy trick to get eval to execute at global scope]). It all depends on the needs of the application.

    I'm not trying to say any one technique is better than the other - to be honest I don't have enough experience with this stuff to make such conclusions


    Pete,

    Sounds like you have a fun project going here

    Hey, I notice we're practically neighbors. I'm only about 2.5 hours from Atlanta.


    Great thread!

  2. #27
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    Pete,

    Sounds like you have a fun project going here

    Hey, I notice we're practically neighbors. I'm only about 2.5 hours from Atlanta.


    Great thread!
    Thanks for the input and the opportunity to learn some more about JS. I've been doing BizTalk work the last few years and this project sort of fell in my lap. And yes, it is fun, like a root canal. Next time you're in the ATL the beers are on me.


  3. #28
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FF supports the .text property because it is standard on script elements.
    Thanks for the link and correcting me. I've never used the .text property, and when I saw it in your code, I went hunting for it, but I couldn't find it at w3c or with google; I could only find it at msdn.

    It's amazing how something so conceptually simple requires such a complicated hack.

  4. #29
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We prototyped IFrames first and the client/customer/boss rejected them out of hand. No real reason other than it looked bad to him - even with no borders.
    I think what jim fraser meant was to load the content into a hidden iframe, which was one of the early methods for making "remote procedure calls". The <script> tags will get parsed as the content loads, and any functions will be available as:

    window.frames["iframeName"].someFunc()

  5. #30
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had something wrong with my code - I've updated the online demo and in the following:
    Edit:


    I removed this code because I still had something wrong! I think I have it right now. See the online demo.


    I was calling removeChild(sa[i]) within the first 'for' loop. But that changed the length of the "live" collection: sa. So I moved the calls to removeChild into another 'for' loop.
    Last edited by MikeFoster; Feb 27, 2007 at 11:19. Reason: Oops!

  6. #31
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I still had it wrong! But I think I've got it worked out now. See the demo.

    But there are still issues, for example:

    We need to assign an ID to the new script element - but what? If this is something that will be called often, on the same page, then the script elements written with innerHTML really need to have IDs. Using their IDs we can come up with an ID for the new script element and remove it if it is already there in the HEAD. Another idea is to create a DIV to hold the new script elements. Then each time xExecScripts is called, remove all script elements from it before inserting more into it. Each of these ideas make assumptions about how xExecScripts will be used.

    phpPete, In your application are these asynchronously-loaded scripts all different? Well, I don't think I'm being too clear about this. For example, one of them might look like this:
    Code:
    <script type='text/javascript'>var appData = ['data', 'data', 'etc.'];</script>
    Now anytime the app needs new data it makes an async Http Request and then the above is written into a DIV. In this scenario we need to remove the previous script element which defined 'appData' before we insert the new one. Am I making any sense?

  7. #32
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For comparison's sake here is a simple example of my iframe method. You can keep loading the same scripts over and over into successive iframes and it still works.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    function HTTPReq() {
    
    	var theAJAX;
    
    	if(window.XMLHttpRequest) {
    		theAJAX = new XMLHttpRequest();
    		if(theAJAX.overrideMimeType) {
    			theAJAX.overrideMimeType('text/xml');
    		}
    	} else if(window.ActiveXObject) {
    		try {
    			theAJAX = new ActiveXObject("Msxml2.XMLHTTP");
    		} catch(e){ 
    			try {
    				theAJAX = new ActiveXObject("Microsoft.XMLHTTP");
    			} catch(e) {
    				theAJAX = false;
    			}
    		}
    	}
    	if(!theAJAX){
    		alert('Giving up :( Cannot create an XMLHTTP instance');
    	}
    	return theAJAX;
    }
    
    function doAjaxPost(paramList,url,onsuccess,onfail) {
    
    	var ajax = HTTPReq();
    	if (!ajax) return;
    
    	var paramArr = [];
    	for (var item in paramList) {
    		// don't handle arrays, objects, functions, etc
    		if (typeof paramList[item] == "string" || typeof paramList[item] == "number") {
    			paramArr.push(encodeURIComponent(item) + "=" + encodeURIComponent(paramList[item]));
    		}
    	}
    	var params = paramArr.join("&");
    
    	ajax.open("POST", url, true);
    	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8"); 
    	ajax.onreadystatechange = function() {
    		if (ajax.readyState == 4) {
    			if (ajax.status == 200 || ajax.status == 0) {
    				onsuccess(ajax.responseText);
    			} else {
    				onfail(ajax.status);
    			}
    		}
    	}
    	ajax.send(params);			
    }
    
    function loadPage() {
    	doAjaxPost({},"test.html",
    		function (ajaxText) {
    			window.frames["stuffGoesHere"].document.write(ajaxText);
    		}, 
    		function (statusCode) {
    			alert("error loading page, status was " + statusCode);
    		}
    	);
    }
    </script>
    </head>
    <body>
    <h2>Iframe test</h2>
    <button onclick="loadPage();">Load a page</button><br/>
    <iframe name="stuffGoesHere"></iframe>
    <h2>end of iframe test</h2>
    </body>
    </html>

  8. #33
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, great! That's great, Jim! I'll be looking more closely at it. This will help provide more balance to the ideas in this thread and show that there's always more than one way of doing something


Bookmarks

Posting Permissions

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