javascript 条件内的 PHP 标头?
我想在 javascript 条件内使用 PHP 发送标头。这可能吗?
if ($('body').hasClass('browserChrome')) {
<?php
header("Content-Type: application/x-chrome-extension");
header('Content-Disposition: attachment; filename="http://example.com/file.crx"');
header("Pragma: no-cache");
header("Expires: 0");
?>
}
I'd like to send headers with PHP, inside of a javascript condition. Is that possible?
if ($('body').hasClass('browserChrome')) {
<?php
header("Content-Type: application/x-chrome-extension");
header('Content-Disposition: attachment; filename="http://example.com/file.crx"');
header("Pragma: no-cache");
header("Expires: 0");
?>
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
那是行不通的。首先,您的 Web 服务器将执行 PHP 脚本,生成带有 JavaScript 的 HTML 页面。然后将其发送到用户的 Web 客户端,该客户端将显示 HTML 并执行 JavaScript。当 JavaScript 解释器到达 PHP 代码时,已经看不到它的踪迹了。
That won't work. First, your web server will execute the PHP script, resulting in an HTML page with JavaScript. That is then sent to the user's web client, which will display the HTML and execute the JavaScript. There is no trace of the PHP code by the time the JavaScript interpreter gets to it.
不,这是不可能的。 (嗯,更像是,它没有意义或没有用,并且可能会导致页面错误。)
在发送页面之前,php 代码在您的服务器上运行,就像鹳嘴里的婴儿一样,它到达客户端浏览器的方式。鹳鸟飞翔的时间很长,经历过好天气和坏天气、风暴、冰雪、狩猎聚会和污染。最后,当您的客户耐心等待新宝宝网页出现时,鹳鸟到达了。
然后,突然,-~ p00f ~- 页面就出现了。此时,页面中的 JavaScript 将运行。
No, it is not possible. (Well, more like, it's not meaningful or useful, and will likely cause page errors.)
The php code runs on your server before the page is sent out, like a baby in a stork bill, on its way to the client browser. The stork flies for a long time, through good weather and bad, storms, ice, snow, hunting parties, and pollution. At long last, the stork reaches your customer as they wait patiently for their new baby web page to appear.
Then, suddenly, -~ p00f ~- and the page arrives. At this point, the JavaScript in the page runs.
如果您需要从 JS 发送一些标头(仅),您可以使用对象 XMLHttpRequest 的方法 setRequestHeader
或可以使用一些 JS 框架方法(jQuery、Prototype 等),
但是正如已经回答的那样,在js中编写php代码是不正确的
if you need send some headers from JS(only), you can use method setRequestHeader of object XMLHttpRequest
or can use some method JS frameworks (jQuery, Prototype, etc,)
But as has already been answered, is not correct write php code in js
看起来好像您正在尝试通过 javascript 下载文件,更像是将文件推送到客户端浏览器。
但是,正如已经回答的那样,代码不可能工作,因为它发生在服务器上,而 js 在客户端上运行。
要实现这一点,您需要使用 javascript 本身(本例中为 ajax)来下载
相关内容。
您可以在此处找到如何从 javascript 下载文件: http://www.codeproject.com/知识库/脚本/FileDownloadUsingJScript.aspx
It seems as if you are trying to download a file through javascript, more like, push the file to the client browser.
However, as it has been answered, its not possible for the code to work, as it happens on the server, and js runs on the client.
To achieve that, you would need to use javascript itself, ajax in this case, to download the
relevant content.
You may find how to download file from javascript here : http://www.codeproject.com/KB/scripting/FileDownloadUsingJScript.aspx