在 javascript if 语句中调用 .php 文件
有没有办法删除 ,将其放入 .php 文件中,然后在其位置使用 javascript if 语句根据屏幕分辨率调用某个 head ?
现在我知道如何执行明显的 if 语句,并且我已经了解了由于客户端与服务器端而在 javascript 中调用 php 的复杂性,但是,在源代码中我可以看到脚本出现,但不起作用。
有什么想法吗?
Is there any way to remove the <head>
, place it in a .php file and then use a javascript if statement in its place to call a certain head depending on screen resolution?
Now I know how to do the obvious if statement, and I've read about the complications of calling php within javascript due to client side vs server side, but, in the source code I can see the script appears, but doesn't work.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从您的评论来看,您说您想要在头部更改的是“主要是样式表”,我认为您希望根据屏幕分辨率和/或其他一些条件应用不同的样式表。您可以这样做,但不是您描述的方式。请尝试类似以下内容:
我通常不会推荐
document.write()
,但对于这种目的,当页面仍在加载时,它比其他替代方案完全没问题且简单。如果您愿意,可以使用document.createElement();
,然后设置适当的属性并将其附加到,但我不会为此烦恼除非您想在页面加载后更改样式表。
您还可以使用条件加载器库,例如 YepNope.js (不用担心它强调加载 JS 文件,它也会做CSS)。
From your comment, where you say that what you want to change in the head is "mainly stylesheets", I take it that you want to apply different stylesheets depending on the screen resolution and/or some other conditions. That you can do, but not the way you describe. Try something like the following instead:
I wouldn't normally recommend
document.write()
, but for this sort of purpose while the page is still loading it's perfectly fine and simpler than the alternatives. If you prefer you can usedocument.createElement();
and then set the appropriate attributes and append it to the<head>
, but I wouldn't bother with that unless you want to change the stylesheets after the page has loaded.You can also use a conditional loader library like YepNope.js (don't worry about its emphasis on loading JS files, it'll do CSS as well).
相反,您可以尝试使用在加载头部后调用的 javascript 来修改头部。 javascript 在页面加载之后或加载时在浏览器上运行,因此显然 head 总是先出现。但您始终可以使用具有不同头部的 javascript 加载另一个页面。
所以你可以全部 page.php?type=1
并且你的 javascript 将加载并根据任何逻辑使用不同的 typeid 重新加载 page.php 。
page.php 将有一个 if else 块,它将向浏览器打印不同的头。
希望这有帮助...
instead you could possibly try modifying the head using javascript that gets called after the head is loaded. javascript is run on browser after or when the page is loaded so obviously the head will always come first. but you can always load another page using a javascript with a different head.
so your can all page.php?type=1
and your javascript will load and based on whatever logic reload page.php with a different typeid.
page.php will have an if else block that will print different head to the browser.
hope this helps...
要调用 PHP 脚本,请查找一些 Ajax 调用。例如下面的代码:
To call a PHP script, look for some Ajax calls. For example the following code: