Reputation: 997
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?
Upvotes: 1
Views: 683
Reputation: 150010
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).
Upvotes: 1
Reputation: 3904
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>
Upvotes: 0
Reputation: 2116
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...
Upvotes: 0