UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

183 lines (134 loc) 5.63 kB
<!-- Generated by Rakefile:build --> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2012-06-16 06:52:05 <br /> @T.Wild: Forgot to let you know the dependency line was added. Thanks! <hr /> <strong> T.Wild </strong> on 2012-01-27 18:03:15 <br /> This should be marked as having a dependency on array_filter (line 23) or be recoded. <hr /> <strong> <a href="http://brett-zamir.me" rel="nofollow">Brett Zamir</a> </strong> on 2010-11-22 07:25:13 <br /> @Joey: Due to security restrictions in JavaScript, this function can only work if the script you are trying to get is on the same domain (or if the site you are targeting is granting HTML5 CORS access and browsers support it). <hr /> <strong> Joey </strong> on 2010-11-21 15:27:04 <br /> I have a problem whit the http header the page give 303 see other, but i need the headers of tha page! But the function don't show how to fix. <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-10 16:46:24 <br /> @ Paulo Ricardo F. Santos: hehe, no problem! Thanks for giving the final function complete though, makes my job a little easier :) <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-12-04 23:20:01 <br /> OMG! Sorry for the flood and ignore my last 2 comments (#7 and #8 - I was quite confused). Here's the function with corrections: <pre><code> function get_headers(url, format) { var req = window.ActiveXObject ? new ActiveXObject(&amp;quot;Microsoft.XMLHTTP&amp;quot;) : new XMLHttpRequest(); if (!req) throw new Error('XMLHttpRequest not supported'); var tmp, headers, pair, i; req.open('HEAD', url, false); req.send(null); if (req.readyState &amp;lt; 3) { return false; } tmp = req.getAllResponseHeaders();alert(tmp); tmp = tmp.split('\n'); tmp = array_filter(tmp, function (value) { return value.substring(1) != ''; }); headers = [req.status + ' ' + req.statusText]; for (i in tmp) { if (format) { pair = tmp[i].split(':'); headers[pair.splice(0, 1)] = pair.join(':').substring(1); } else { headers[headers.length] = tmp[i]; } } return headers; } </code></pre> About the Opera problem, I don't know how to fix it. In my tests, Opera 9.62 returned a empty string in statusText property and getAllResponseHeaders() function - only status property returned as expected. Both Firefox 3 and IE 7 worked perfectly. :) <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-12-04 16:51:53 <br /> Ah! This line: <pre><code>headers[pair.splice(0, 1)] = pair.join(':').replace(/^\s+|\s+$/g, '');</code></pre> might be only this, there's no difference: <pre><code>headers[pair.splice(0, 1)] = pair.join(':');</code></pre> And as far I've noted, to work properly in Opera, the AJAX request should be asynchronized, as here: http://www.ceciliadassi.com/get_headers2.js. Synchronized requests returns false in the most of times. ;/ <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-12-04 16:17:03 <br /> Hmm, might be useful replace <pre><code>tmp.pop();</code></pre> to <pre><code>tmp = array_filter(tmp, function (str) { return str.replace(/^\s+|\s+$/g, '') != ''; });</code></pre> to avoid any unexpected problem. ;) <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-12-04 14:54:40 <br /> Oops, sorry for the inconvenient - it's a bit buggy and I don't know how it worked before. This snippet should fixes all the problems: <pre><code> tmp = req.getAllResponseHeaders().split('\n'); tmp.pop(); headers = [req.status + ' ' + req.statusText]; for (i in tmp) { if (format) { pair = tmp[i].split(':'); headers[pair.splice(0, 1)] = pair.join(':').replace(/^\s+|\s+$/g, ''); } else { headers[headers.length] = tmp[i]; } }</code></pre> Here's a temporary demo - working as expected: http://www.ceciliadassi.com/get_headers.html []'s <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-03 14:14:46 <br /> @ Paulo Ricardo F. Santos: I'm testing from console using the ./phpjstest.php file in the _tools directory in svn. But what's strange is that code from file_size (which I thought was identical) does not break. <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-12-03 13:26:51 <br /> @ Kevin: Ah! Well... how you are testing? AFAIK, there's no way to test it locally or request a cross-domain address. I've tested it on my test domain, requesting a file from same server. ;) <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-03 12:58:34 <br /> @ Paulo Ricardo F. Santos: Well I haven't got it working yet, so that's why the examples are still in a copy-pasted state. <hr /> <strong> Paulo Ricardo F. Santos </strong> on 2008-12-03 12:56:47 <br /> @ Kevin: Hmm, great idea, I forgot this possible abnormal behavior! &amp;gt;.&amp;lt; Ah!, man, I don't understood your example. Why it should returns 123? o.O <hr /> <strong> <a href="http://kevin.vanzonneveld.net" rel="nofollow">Kevin van Zonneveld</a> </strong> on 2008-12-03 11:34:51 <br /> @ Paulo Ricardo F. Santos: In my test suite: phpjstest.php, readyState would stay at 1, I had to build a condition &amp;lt; 3 to avoid the function from crashing. Very strange, because the file_exists function uses the same code and that works ok. <hr />