I'm trying to get my page to scroll to a certain number of pixels down the page when it loads. I'm editing a wordpress template that I created so it's in PHP.
I've got:
<?php
echo "<body onload='window.scroll(0,400)'>";
?>
at the top of the php file but it's not working. what do I need to do to make this work?
Use this code in your header.php on your child theme or any similar.
<script type="text/javascript">
$( document ).ready(function() {
window.scroll(0,400);
});
</script>
Don't forget insert jquery in your head if this code not working
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
The onload event attribute is expecting a function, and you can do that inline with javascript: in the attribute...
<?php
echo "<body onload='javascript:window.scroll(0,400);'>";
?>
Related
I have a problem with loading another page above my page. I did try the iframe method, and it kind of works. Iframe will show over full screen but the content that is on my page will also show bellow that iframe when I scroll to the bottom of the page... Is there any way to put iframe on top of this content or there is better way with maybe lightbox or something like that. I'm new to the coding game and was having hard time with just iframe to show over whole page...
This is the code that used in template that I created for this purpose.
<?php
/**
Template Name: Content Only
*/
?>
<html>
<head>
<title><?php wp_title( '|', true, 'right' ); bloginfo('url'); ?></title>
<style>
html,body,div,iframe {height:100%;}
p {position:relative;overflow:hidden;}
iframe {border:none;width:100%;}
body {margin:0;padding:0;}
</style>
</head>
<body>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); endwhile; ?>
</body>
</html>
Then I insert iframe in text editor on page I want to show iframe and there is iframe but also the content bellow the iframe (and double scroll bar).
I would like to have a code that will put other site above (in front of) my site.
This must be a comment:What exactly are trying to achieve?
By your reference to lightbox...I'am guessing you are trying to create a modal.Try these:http://getbootstrap.com/javascript/#modals
http://www.w3schools.com/bootstrap/bootstrap_modal.asp
Thank you for your answer but I managed to do what had to be done by tweaking the code. Everything is working like it should.
This is what i changed:
html,body,div,iframe {height:100%;overflow:hidden;}
p {overflow:hidden;}
iframe {z-index:1000;border:none;width:100%;}
I can put php code anywhere in my php file, but if i put the php code within the tags it simply will not work.... What am I doing wrong?
<html>
<head>
<?php
$hello3 = 'hello3';
echo 'hello1';
?>
<script>
<?php echo 'hello2'; ?>
$(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
</script>
<?php echo $hello3; ?>
</head>
<body>
<button type="button" id="target">Press Me</button>
</body>
</html>
when I execute this page I get this displayed on my screen:
hello1hello3 (then the button)
the echo within the script tags will not execute... at all.
This is the HTML equivalent of your code:
<html>
<head>
hello1
<script>
hello2
$(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
</script>
hello3
</head>
<body>
<button type="button" id="target">Press Me</button>
</body>
</html>
What's wrong with your output?
The output of your PHP, within the script element, looks like this:
hello2 $(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
It immediately falls over (with a reference error) because you try to use the variable hello2 without declaring it.
Were that not the cause, it would then fall over because you try to use the variable $ without declaring it (i.e. by including jQuery in your script).
The PHP is executing fine. You are just writing PHP that generates completely broken JavaScript.
Yes this does execute. But items in the <script> tag are not displayed on the screen unless done so by a function or method. You just have items like hello2; in JavaScript. For example what would you expect so see if you had this:
<script>
hello1;
</script>
Well, nothing would be on the screen. You are just creating global variables, which are not defined, so what would happen? Try adding some code for example a simple alert:
<script>
<?php echo 'alert("hello")'; ?>
</script>
When writing PHP the items you echo would be exactly the same as you were writing JavaScript normally. So the same rules for writing JavaScript apply. The above item would look like so after PHP executes:
<script>
alert("hello")
</script>
i tried to do like this. i think its worthless
here my code..
<?php
$screen = '<script type="text/javascript">document.write(screen.width);</script>';
$echo $screen;
?>
this javascript code is work without php code..
<script type="text/javascript">
document.write(screen.width);
</script>
but i want to get only width from variable
i tried to like this get screen size from php.. but its not work.. someone can help me for do this one.. i want to variable for screen width.. if have another good method plz give me answer for this one.. thanks.
As mentioned in one of the comment, you need to differentiate client and server.
Though to make things simple, if you want to get screen width on server side, you can do the following :
Index.php
<?php
session_start();
if(!isset($_GET['width']) && !isset($_SESSION['screen.width'])){
?>
<html>
<head>
<script type="text/javascript">
location.replace('http://example.com/?width='+screen.width);
</script>
</head>
<body></body>
</html>
<?php
die();
}
if(isset($_GET['width'])){
$_SESSION['screen.width'] = (int) $_GET['width'];
}
echo "screen width : ".$_SESSION['screen.width'];
?>
I'm trying to add the bgcolor attribute to the body tag with PHP.
Using getElement() and setAttribute() won't work in PHP.
Does anyone know a solution?I want to change the background color of the body with PHP, so any other solutions using PHP would be great too.
Edit:
For an assignment that's way outdated we HAVE to use PHP to change the backgroundcolor and we need to use bgcolor. With a POST form with some radiobuttons and a Submit button we need to change the bgcolor. Up to this I used echo to ADD a tag after there is a tag already. Any ideas on that?
Edit2:
I tried to echo the in the bgcolor attribute, but that seems to stop the full page from loading, so that doesn't work unfortunately.
PHP is server side! If you want to do it with php simple echo it like this
<?php echo '<body style="background-color:#000">'; ?>
otherwise you will need to do it in javascript with
document.getElementsByTagName('body')[0].style.backgroundColor = '#000';
hope that help
A simple solution is to inject this somewhere in your html:
<style type="text/css">
body {
background-color: <?php echo $color; ?> !important;
}
</style>
Then before this code is run, define $color to hold a color value like #cccccc.
Well, actually you shoudn't use bgcolor anymore. Instead, give a css style to the body tag:
<?php
$color = "#004422";
?>
<body style="background-color:<?php echo $color; ?>">
<!-- CONTENT -->
</body>
You can use inline styles (wheter in <head>, wheter in <body> with scoped attribute) or variables.
...
<style>
body {
background-color: <?php echo $body_bgcolor; ?>
}
</style>
...
or
<body style="background-color: <?php echo $body_bgcolor; ?>">
...
</body>
I have a PHP code:
if($billing_total>$limit_to_send){
echo '<script type="text/javascript">
window.onload = function() {
alert("Sorry, you do not have enough credit");
}
</script>';
When I am printing this message, it is being printed at the beginning of the PHP page as below:
<script type="text/javascript">
window.onload = function() {
alert("Sorry, you do not have enough credit");
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-----------------------------------------------------------
This caused the header (logo) of my page in the browser to move down one line.
and the page will look very bad because all the items there will be moved down one line.
I hope it is clear to you. Please any solution ?
===========================================================================
Thanks for All ...
Solution:
$alert_message=<script type="text/javascript">
window.onload = function() {
alert("Sorry, you do not have enough credit");
}
</script>
Printing $alert_message somewhere in the HTML code before the body tag ^_^
Make sure you never output anything before the DTD (doctype declaration).
See this question for more information...
The doctype declaration must be the first element of your html page, it's from what the browser decides how to handle the rest of the html code. Outputting anything before that will probably put your browser in quirks mode so you can't be sure how the browser will render your page.
How to avoid this?
The echo command gets executed as its line is reached, and it seems that the rest of your html code follows after that.
You could either
move the html DTD and header to the top of your php (but sometimes that is not possible) OR
store the error html in a variable, so instead of echo '<script ... do $errorhtml = '<script ... and output that string, if not empty, at a specific place in the head or body generating code of your php.
If you have no control over the original source, you could consider redirecting to an error page with its own html DTD, header and body which you can design as fits you best.
Either append die() into the if codeblock or have your php print the script somewhere in the body or head.
This shows a bad design of your application. I would suggest you change it to something like:
$errors = array();
if($billing_total>$limit_to_send){
$errors[] = 'Sorry, you do not have enough credit';
}
Then on your HTML file, before the <body> tag closes, read your array and display any errors
<?php if(is_array($errors)): ?>
<script type="text/javascript">
<?php foreach($errors as $error): ?>
alert('<?php echo $error; ?>');
<?php endforeach; ?>
</script>
<?php endif; ?>