1

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?

9
  • 6
    This is not how it's done. What is inside the head that you need to change depending on screen resolution? Commented Nov 25, 2011 at 10:07
  • Mainly style sheets. I've done it a different way previously but it gets a bit confusing when applying different style sheets to over 7 devices. Some of them don't want to play ball when using media="screen .... Commented Nov 25, 2011 at 10:12
  • 1
    A head should always be apparent. Your solution doesn't really make sense; perhaps you'd want to explain your initial problem. Commented Nov 25, 2011 at 10:12
  • 2
    You are getting many concept wrong: JavaScript is a client-side language and is called after the page is loaded (or at the very least after the head part is received). PHP on the other hand is a servr-side language and it's called before the page is received. You can get stuff from PHP via JavaScript and Ajax, but you still have to do it after the head is received. You probably shouldn't put resolution-dependent code (I'm supposing you need to modify CSS) in web pages anyway. Commented Nov 25, 2011 at 10:13
  • 3
    Short answer: no. Longer answer: nooooooo. More detailed answer: you're heading in a direction of insanity. You've also written a post that describes a proposed solution and asks how to implement it, rather than stating a problem and requesting a solution. That's not necessarily inherently wrong, but it's been your undoing here because your proposed solution makes little sense and the original problem remains invisible to us. Commented Nov 25, 2011 at 10:14

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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...

2 Comments

So would the page load and then it re-directs or will the head change before the page loads?
Yes the page will have to load once where you do your checks, else your script wont run. But the redirect would work fast enough. You can inline javascript calls so you dont need it to be triggered. for example <scrip>function test() {} test();</script> This will call test as the page loads and you could have a redirect in there
0

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>

1 Comment

This is what I have been looking into, might have to try this if Sid's resolution doesn't help. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.