skip to content

Mootools ‘ajax’ request for local files

Ok, ok, I know it’s a contradiction in terms, but this is a problem encountered by a lot of newbies, and sometimes a feature like this would be useful to be able to copy the whole js into a local folder, and run it form here, without being forced to start a web server on the local machine.

How can this be accomplished without too much hassle or code change?

I created a function that tests the protocol used by the browser, and decides how to load the requested file, with xmlhttprequest or iframe.

Please be warned that this is just an exercise, and not a final solution. It can be improved and it should be :)

The example is here.
Obviously this will use the xmlhttprequest, but if you download this zip, and unzip it in a local folder, you will see working the iframe method.

The code is below:

	function get_content(url,update) {
					if ( document.location.protocol == "file:" ) {
						// console.log('local');
						var ifr = new IFrame({
							src:url,
							events:{
								load: function() {
									$(update).set('html', this.contentWindow.document.body.innerHTML );
								}
							}
						}).inject(document.body);
					}
					else {
						// console.log('server');
						var myHTMLRequest = new Request.HTML({
							update:update,
						}).get(url);
					}
		}

Let me know your thoughts.

 

2 Responses

[...] nos muestra una solución para leer fichero cuando ejecutamos HTML desde nuestro local. Detectando si la página está siendo ejecutado en un entorno local mediante el protocolo file: de [...]

1.22.2010

Thank you! Nice post.. works for me..


Switch to our mobile site