jquery.js ã® load é¢æ°ã§ãã¼ãããã¹ã¯ãªãããä¸é¨ãã©ã¦ã¶ã§å®è¡ã§ããªã件
IE6 㨠mac safari(windowsçã§ã¯ãªãã大ä¸å¤«) ãChrome ã§ã ajaxã®ä¸ã§ãã¼ãããã¹ã¯ãªãããå¼ã³åºããªãã
å
·ä½çã«ã¯ã以ä¸ã®ãããªå ´åã loadãã abc.htmlã®ãã¿ã³ãæ¼ãã¨hello()ãå¼ã³åºãããã¨ãã話ã
ç°¡åãªæ¦è¦
---main.html--------- ã³ã³ãã³ãabc.htmlããã¼ããã. $('#nanka').load('abc.html');
---abc.html---- æã£ã¦ããã³ã³ãã³ãã®ãã¿ã³ãæ¼ã㨠javascript ãå®è¡ããã¾ãã? <input type="button" onclick="hello()"> function hello() { alert("ãããå®è¡ãããã?"); }
ãã¢ãµã¤ã!!
http://rtilabs.net/files/2008_09_16/jquery_load_test_main.html
ã½ã¼ã¹ã§èªããã
prototype.js ã¯ãã®åé¡ããã¾ãåé¿ãã¦ããã
jquery.js ã® load ãå©ç¨ããã¨ãã«çºçãã¾ãã
jquery.js ã® load é¢æ°ã®ä¸ãè¦ã¦ã¿ãã¨ããããªã£ã¦ãã¾ãã
// Request the remote document complete: function(res, status){ // If successful, inject the HTML into all the matched elements if ( status == "success" || status == "notmodified" ) // See if a selector was specified self.html( selector ? // Create a dummy div to hold the results jQuery("<div/>") // inject the contents of the document in, removing the scripts // to avoid any 'Permission Denied' errors in IE .append(res.responseText.replace(/<script(.|\s)*?\/script>/g, "")) // Locate the specified elements .find(selector) : // If not, just inject the full result res.responseText ); self.each( callback, [res.responseText, status, res] ); }
ããããã¨ãã¡ããã¡ããã¦ããã®ã§ä½¿ããªãã¨ãããåé¤ãã¦ç°¡ç¥åããã¨ã
complete: function(res, status){ // If successful, inject the HTML into all the matched elements if ( status == "success" || status == "notmodified" ) // See if a selector was specified self.html( res.responseText ); self.each( callback, [res.responseText, status, res] ); }
ãããªæãã«ãªãã¾ãã
ã¤ã¾ããåãåã£ãã³ã³ãã³ãã self.html ããããã«ã innerHTML ã«æ¸¡ãã¦ããã¨ãããã¨ã§ãã
åé¡ã¯ãã®ã¨ãã«javascripté¢æ°ãå«ã¾ãã¦ããã¨ãã«ã©ãåä½ãããã¨ãããã¨ã§ãã
ä¸è¨ã® IE6 ã mac safari ã Chrome ã¯ãé¢æ°å®ç¾©ãç¡è¦ãã¾ãã
æ£å¼ãªä»æ§ãã©ããªã£ã¦ããã®ãã¯è¬ã§ãããé ãåºãå°é家æ°åãã® moziila(firefox)ã§åä½ããã®ã§æ£ãããã§ãããwww
IE7ã§ãåä½ãããããIE6ã®ãã°ãã£ãã¯ã¹?
ãã§ããããªå°ã£ãåä½ããã¦ããããã©ã¦ã¶ã¯ã IE6 㨠mac safari ã ãã ã¨æã£ããã§ããã©ã Chrome ã§ãåãåé¡ãèµ·ãã¦ããã®ã§ãå¿ããªããã¡ã«æ¸ãã¨ãã¾ãã
解決æ³
ã¾ã¼ prototype.js ã ã¨åããã ããã prototype.js ããã½ã¼ã¹ãæã£ã¦ãã¦é©å½ã«ãã¼ã¸ããã°okã
ã©ã¤ã»ã³ã¹çã«ã両æ¹ã¨ãMITã©ã¤ã»ã³ã¹ãé¸æã§ãããã ããã主ç¹ãæè¨ãã¦æµç¨ãã¾ãããããããã¨æ¸ãã¦ããã°å¤§ä¸å¤«ã§ããããå¤åã
//ã¹ã¯ãªããã®åé¤ prototype.js ã®ã½ã¼ã¹ãã(ä¸é¨ç·¨é) function _stripScripts(scriptsString) { var ScriptFragment = '<script[^>]*>([\\S\\s]*?)<\/script>'; return scriptsString.replace(new RegExp(ScriptFragment, 'img'), ''); } //ã¹ã¯ãªããã ããæ½åºããé åã«æ ¼ç´ prototype.jsã®ã½ã¼ã¹ãã(ä¸é¨ç·¨é) function _extractScripts(scriptsString) { var ScriptFragment = '<script[^>]*>([\\S\\s]*?)<\/script>'; var matchAll = new RegExp(ScriptFragment, 'img'); var matchOne = new RegExp(ScriptFragment, 'im'); return jQuery.map( scriptsString.match(matchAll) || [] , function(scriptTag) { return (scriptTag.match(matchOne) || ['', ''])[1]; } ); } //ã¹ã¯ãªãããå®è¡ãã. prototype.jsã®ã½ã¼ã¹ãã(ä¸é¨ç·¨é) function _evalScripts(scriptsString) { jQuery.map( _extractScripts(scriptsString) , function(script) { jQuery.globalEval(script); return true; } ); } //IE7? function isIE7() { return ( typeof document.documentElement.style.msInterpolationMode != 'undefined' ); } //MAC function isMac() { var agent = navigator.userAgent.toLowerCase(); return agent.indexOf("macintosh") != -1 || agent.indexOf("mac os") != -1; } //Chrome function isChrome() { var agent = navigator.userAgent.toLowerCase(); return agent.indexOf("chrome") != -1 ; } //ããã©ãã ããã selector ã®é¨åã¨ã¯ã°ã£ããåé¤! ã¾ãã«å¤é jquery.jsãã(ä¸é¨ç·¨é) jQuery.fn.extend({ super_load: function( url, params, callback ) { callback = callback || function(){}; // Default to a GET request var type = "GET"; // If the second parameter was provided if ( params ) // If it's a function if ( jQuery.isFunction( params ) ) { // We assume that it's the callback callback = params; params = null; // Otherwise, build a param string } else { params = jQuery.param( params ); type = "POST"; } var self = this; // Request the remote document jQuery.ajax({ url: url, type: type, dataType: "html", data: params, complete: function(res, status){ // If successful, inject the HTML into all the matched elements if ( status == "success" || status == "notmodified" ) { //IE7æªæºã¯ javascript ãæ¸ãè¾¼ããã¨ããã¨ã¨ã©ã¼ã«ãªãã®ã§ã //prototype.jsã®ããæ¹ã§ãjavascriptã ããå®è¡ããå®è¡ãã¦ãããå¿ è¦ããã. //Mac Safari ãåæ§ã®ãã°ãããã windows safariã¯ç´ã£ã¦ããã®ã«ãªãã§? //Chrome! ãåãã! if ( (jQuery.browser.msie && !isIE7()) || (jQuery.browser.safari && isMac() ) || ( isChrome() ) ) { //HTMLã ããæ¸ãè¾¼ã self.html(_stripScripts(res.responseText)); //javascriptãå®è¡ãã _evalScripts(res.responseText); } else { //IE7以ä¸ã¾ãã¯ãFFãOperaã¯jQueryã®ãã¤ãã®ããæ¹ã self.html(res.responseText); } } //ã¨ã©ã¼ã®ã¨ããã³ã¼ã«ããã¯ãããã! ã³ã¼ã«ãã㯠self.each( callback, [res.responseText, status, res] ); } }); return this; } });
ãããªæãã«ãã¦ãå¾ã¯ã load ã§å¼ã³åºãã¦ããã¨ããã super_load ã«å¤ããã°åé¡ãªãåä½ãããã
ãã¨ãçµå± innerHTMLã§æã£ã¦ããjavascript ã®é¢æ°ãå®è¡ã§ããã¹ããªã®ãã§ããªãã¹ããªã®ããä»æ§ãç¥ã£ã¦ããå°é家ã¨å°é家æ°åãã®ã¨ãã¤äººã¯æãã¦ãã ããã