Remember Page Position Using Two Lines of Script - php

In all major browsers, except Internet Explorer, the following script returns the page to its previous vertical position on reload:
<?php $y = $_COOKIE["y"]; ?> //in head tag before any output
and
<?php
print "<body onScroll=\"document.cookie='y=' + window.pageYOffset\" onLoad='window.scrollTo(0,$y)'>";
Can someone please tell me how I would modify this code to remember the page's vertical position in IE? Thanks.

From w3Schools :
IE 8 and earlier does not support this property, but may use "document.body.scrollLeft" and "document.body.scrollTop" instead.

I use the following code to do basic IE/not-IE browser detection:
if(document.all) { //if IE
//code
} else { //if not IE
//code
}
You should be able to combine this with AlecTMH's document.body.scrollLeft and document.body.scrollTop suggestion to get where you're going. But you're likely going to have to write a function for it and then call that in onScroll().
I'm not exactly a JavaScript wiz, but...
function blah() {
if(document.all) { //if IE
document.cookie='y=' + document.body.scrollTop
} else { //if not IE
document.cookie='y=' + window.pageYOffset
}
}
...might almost be functional code.

Related

Execute jquery in if statement

I got a login system, and what I want to do is to hide a div and show another div when the user types the incorrect login details. However it doesn't seem to work.
if(...) { // here I check if the user enters the correct information
... //much code inside
} else { // if incorrect info, do this
echo "<script>$('#jqueryhide2').hide();
$('#jqueryhide').show();</script>";
}
Tried to google a bit but can't find anything that could solve my problem.
put this code and also include your Jquery file -(dont forget)
echo "<script>
$('document').ready(function(){
$('#jqueryhide2').hide();
$('#jqueryhide').show();
});</script>";
I think you should missing the Onclick function like this:
<script>
$('loginId').Onclick(function(){
$('#jqueryhide2').hide();
$('#jqueryhide').show();
});</script>";
You don't need the script tags inside your statement. You can simply do the following. ( Make sure jqueryhide and jqueryhide2 are a part of the DOM. )
if (blah == blah )
{
$('#jqueryhide2').show();
$('#jqueryhide').hide();
}
else
{
$('#jqueryhide2').hide();
$('#jqueryhide').show();
}

How do I target IE 10 and up with PHP?

I'm using the following code to add some conditional styles to a webpage (I use traditional conditional comments for IE 9 and down):
<?php if (stripos($_SERVER['HTTP_USER_AGENT'], 'MSIE 10')) { ?>
<link href="/css/ie9.css" media="screen" rel="stylesheet" />
<?php } ?>
The problem is that the style sheet is still not being applied to some versions of IE 10, and definitely not IE 11 preview. Is there a way to target all versions of IE 10 and up? I've only seen formatting that uses [1-9], but [10-11] doesn't seem to be working.
Put some code in your user agent check to see if anything is being returned at all, it may be returning something other than "MSIE 10", it is possible that IE 10+ returns simply IE or some other statement. I have been using javascript to do this very same thing. (despite the cat-calls from the purists about using conditional comments)
Here is my javascript:
function isIE(ver)
{
var myNav = navigator.userAgent.toLowerCase();
var IEVer = parseInt(myNav.split('msie')[1]);
if ((typeof IEVer == "undefined") || (IEVer == null)) {
IEVer = 0;
}
if IEVer = ver {
return true;
}
else {
return false;
}
}
Then in where i'm adding stylesheets is do something like:
if isIE(10) { do something }
Update: according to Microsoft, you should use feature detection instead of browser detection: http://msdn.microsoft.com/en-us/library/ie/hh273397%28v=vs.85%29.aspx

View full site with cookies, javascript and mobile redirect

I have a website(www.website.com) and a mobile site (m.website.com) and I'm trying to allow users to "View Full Site" if they want from mobile. Currently I am using some Javascript to check screen width on full site then redirecting to mobile.
if (screen.width <= 699) {
document.location = "http://m.website.com";
}
This works fine. On the mobile site there is a "View Full Site" button. When you click this it redirects you to my script "force_desktop.php" which sets a cookie and then sends you to the main site.
<?php
setcookie("force_desktop", "1");
header("Location: http://www.mywebsite.com");
?>
Now that we set a cookie and redirected to the main website we need to check for the cookie instead of just checking for the screen width.
Logic
If Cookie force_desktop is found
Then exit
Else
run screen size test
Here is the code I attempted to use but doesn't work. This code is placed in my head.php file which will be run on every page and is placed between the script opening and closing tags.
Attempt 1
if($_COOKIE['force_desktop'] == '1' {
exit;
} else if($_COOKIE['force_desktop'] != '1' {
if (screen.width <= 699) {
document.location = "http://m.website.com";
}
};
Attempt 2
if(isset ($_COOKIE["force_desktop"])){
exit;
else
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";
};
Alternative logic that could work
IF Cookie force_desktop is not found AND screen.width <= 699
Then redirect to m.myseite.com
Else
Exit
Note
I have run the following script to make sure a cookie is being placed, and it is.
<?php
print_r($_COOKIE);
?>
I've run out of ideas and know my coding isn't correct, especially the If/Else statement within the If/Else statement. I also am not sure if it is better to use the "isset" to see if the cookie is being used or ($_COOKIES['variable'] == "#"). I'd really appreciate some good feedback on this one.
Thanks in advance for your help and suggestions,
Matt
You're mixing JavaScript and PHP. You're trying to use screen.width in your PHP script, which doesn't make sense. You need to use an echo statement to output the JavaScript into the page. It'll then check the user's screen resolution and do the redirect.
Try this:
if(isset ($_COOKIE["force_desktop"])){
exit;
}
else
{
echo '<script>
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";}
</script>';
};
you should do this test at the top of the php page, and for sure you cannot mix php and java script like this
u can alter this code like
<?php
$flag = 0;
if(isset($_COOKIE['force_desktop']))$flag++;
?>
later in the page use the code as soon as <head> tag starts
..
..
<head>
<?php
if(!$flag){
echo '<script>
if (screen.width <= 699) {
document.location = "http://m.mywebsite.com";
</script>';
}
?>
You cannot mix javascript and PHP, javascript is front end and PHP is back end. Try something like this:
if( $something ){
Header("Location: somewhere.php");
}

When my XAJAX call, via PHP, returns NULL - it gets displayed on IE (but it should display nothing), how to correct this?

Please review the code fragments below:
First, the Javascript call:
function getNewSelect(property_id){
xajax_getSelectDropDown(unique_id);
$(".chzn-select").chosen();
}
Next, the PHP XAJAX function:
function getSelectDropDown($unique_id){
//CODE ABOVE THIS POINT HAS BEEN REMOVED, IT IS IRRELEVENT FOR THIS EXAMPLE
//Code for if no matches were found:
$html2 = NULL;
$objResponse = new xajaxResponse();
if(mysql_num_rows($result) > 0){
$objResponse->assign('subDiv', 'innerHTML', $html);
}else{
$objResponse->assign('subDiv', 'innerHTML', $html2);
}
$objResponse->script("$('.chzn-select-ajax').chosen();");
return $objResponse;
}
Finally here's the HTML code:
<div id="subDiv"></div>
Now, when the above PHP function goes into the else and assigns $html2 - I expect nothing to be displayed to the user. On most browsers, that's fine and it works perfectly. Unfortunately on internet explorer (IE 9 I can confirm, I haven't been able to test other versions), it actually displays the following where the DIV call:
null
If a result is found and it does not process the else, then it works correctly on all browsers and shows a new drop down as it's supposed to.
What can I do to fix this problem? Is this a known issue with IE? Thanks.
I want to add so there's no confusion, the CHZN call is just to add a style to the dropdown, as found here: http://harvesthq.github.com/chosen/
Better use this:
if(mysql_num_rows($result) > 0){
$objResponse->assign('subDiv', 'innerHTML', $html);
}else{
$objResponse->assign('subDiv', 'innerHTML', '');
}
IE is showing null because $html2 = NULL......better try it with $html2 = " " or something like that ;)
Saludos.

Running a function by typing in URL

I have searched all over the internet to run a function from URL and found out it is not possible.
I am asking if there is another way to do this. I will explain below what I am trying to achieve.
On my website I have several functions that when a hyperlink is clicked on that cracks.php page, the main content changes accordingly. Now I want to direct people from a single URL to that cracks.php page with the function already called.
Example - www.example.com/cracks.php?AC ( which will call a function named AC and the content changes before the page loads)
Ive found this method below, but couldnt get it to work.
if(document.location.search == '?AC')
{
AC();
}
Sorry for the messy code on the website. Thanks for reading, any help would be appreciated.
You can call www.example.com/cracks.php?do=AC and then get do with $doMethod = $_GET['do'];. What you then do is, use a switch function or a few ifs to check and execute when e.g. $doMethod equals AC.
Like this:
$doMethod = $_GET['do'];
switch($doMethod)
{
case "AC":
//some random stuff to do
break;
case "BD":
//some random stuff to do
break;
case "XY":
//some random stuff to do
break;
default:
break;
}
That depends if you need to do that dynamically or you can do it hard coded. Because that hard coded is too simple (with if's and switches), what you have to do is:
$functionsList = Array('func1', 'func2');
function func1(){
echo '1';
}
function func2(){
echo '2';
}
if (function_exists($_GET['f']) and in_array($_GET['f'], $functionsList)){
call_user_func($_GET['f']);
}
Then call your_file_name.php?f=func1 and your_file_name.php?f=func2 and you'll see different outputs.
With the help of Mark Koopman I managed to use his Javascript method and it worked like I wanted.
So heres the method in Javascript:
<html>
<body>
<script>
function handleOnload()
{
if(location.search == "?AC")
alert("the query string is " + location.search);
}
window.onload=handleOnload;
</script>
</body>
</html>

Categories