javascript 条件内的 PHP 标头?

发布于 2024-10-18 06:57:55 字数 353 浏览 2 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄风 2024-10-25 06:57:55

那是行不通的。首先,您的 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.

帅气尐潴 2024-10-25 06:57:55

不,这是不可能的。 (嗯,更像是,它没有意义或没有用,并且可能会导致页面错误。)

在发送页面之前,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.

幻想少年梦 2024-10-25 06:57:55

如果您需要从 JS 发送一些标头(仅),您可以使用对象 XMLHttpRequest 的方法 setRequestHeader

new XMLHttpRequest(); 
//or 
new ActiveXObject(...) for IE
...
xmlHttp.open(... 
xmlHttp.setRequestHeader("Cache-Control", "no-cache, must-revalidate");  
xmlHttp.setRequestHeader("Pragma", "no-cache"); 

或可以使用一些 JS 框架方法(jQuery、Prototype 等),

但是正如已经回答的那样,在js中编写php代码是不正确的

if you need send some headers from JS(only), you can use method setRequestHeader of object XMLHttpRequest

new XMLHttpRequest(); 
//or 
new ActiveXObject(...) for IE
...
xmlHttp.open(... 
xmlHttp.setRequestHeader("Cache-Control", "no-cache, must-revalidate");  
xmlHttp.setRequestHeader("Pragma", "no-cache"); 

or can use some method JS frameworks (jQuery, Prototype, etc,)

But as has already been answered, is not correct write php code in js

心安伴我暖 2024-10-25 06:57:55

看起来好像您正在尝试通过 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文