在 javascript if 语句中调用 .php 文件

发布于 2024-12-18 03:39:16 字数 200 浏览 5 评论 0原文

有没有办法删除 ,将其放入 .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 技术交流群。

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

发布评论

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

评论(3

撩人痒 2024-12-25 03:39:16

从您的评论来看,您说您想要在头部更改的是“主要是样式表”,我认为您希望根据屏幕分辨率和/或其他一些条件应用不同的样式表。您可以这样做,但不是您描述的方式。请尝试类似以下内容:

<html>
<head>
<script>
if (whateveryourconditionis) {
   document.write('<link rel="stylesheet" href="sheet1.css" type="text/css">');
} else (someothercondition) {
   document.write('<link rel="stylesheet" href="sheet2.css" type="text/css">');
   document.write('<link rel="stylesheet" href="sheet3.css" type="text/css">');
} else {
   document.write('<link rel="stylesheet" href="default.css" type="text/css">');
}
</script>
</head>
<body>
</body>
</html>

我通常不会推荐 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:

<html>
<head>
<script>
if (whateveryourconditionis) {
   document.write('<link rel="stylesheet" href="sheet1.css" type="text/css">');
} else (someothercondition) {
   document.write('<link rel="stylesheet" href="sheet2.css" type="text/css">');
   document.write('<link rel="stylesheet" href="sheet3.css" type="text/css">');
} else {
   document.write('<link rel="stylesheet" href="default.css" type="text/css">');
}
</script>
</head>
<body>
</body>
</html>

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 use document.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).

半衾梦 2024-12-25 03:39:16

相反,您可以尝试使用在加载头部后调用的 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...

时间海 2024-12-25 03:39:16

要调用 PHP 脚本,请查找一些 Ajax 调用。例如下面的代码:

<script type="text/javascript">
function _CallPHP() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            alert("PHP has been executed");
        }
    }

    xmlhttp.open("GET","/phpscript.php",true);
    xmlhttp.send();
}
</script>

To call a PHP script, look for some Ajax calls. For example the following code:

<script type="text/javascript">
function _CallPHP() {
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            alert("PHP has been executed");
        }
    }

    xmlhttp.open("GET","/phpscript.php",true);
    xmlhttp.send();
}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文