Based on the result of a PHP if statement, I want to change the source attribute of an iframe.
I have the following:
<?PHP
if {...}
else {
echo "<script>";
echo "document.getElementById('video-embed').setAttribute('src', 'https://www.youtube.com/embed/dQw4w9WgXcQ');";
echo "</script>";
}
?>
<!DOCTYPE html>
<body>
<div class="wrapper">
<iframe id="video-embed" src="...some other youtube video"></iframe>
</div>
</body>
</html>
I know that executing valid JavaScript from the if statement is at least feasible, as I've succesfully tested JS alerts. Everything I've seen online has seemed to indicate that the syntax in question for the line "document.getElem..." is valid. Any help would be appreciated.
You have the Javascript before the HTML that creates the element that it's trying to modify, so getElementById() isn't finding the element. Move the PHP code that adds the JS to the end of the HTML, so that the ID will be found. Or put the Javascript in the window's load handler.
<?PHP
if {...}
else {
echo "<script>";
echo "window.addEventListener('load', function() {";
echo "document.getElementById('video-embed').setAttribute('src', 'https://www.youtube.com/embed/dQw4w9WgXcQ');";
echo "});";
echo "</script>";
}
?>
I am trying to include some JavaScript to just one single page of a WordPress based website. Basically, what I've done is in the header.php of the theme, I've put the following:
<?php if( is_page('17')) { ?>
<!--Start of Zopim Live Chat Script-->
<script type="text/javascript">
window.$zopim||(function(d,s){var z=$zopim=function(c){z._.push(c)},$=z.s=
d.createElement(s),e=d.getElementsByTagName(s)[0];z.set=function(o){z.set.
_.push(o)};z._=[];z.set._=[];$.async=!0;$.setAttribute("charset","utf-8");
$.src="//v2.zopim.com/?2pL2gooCVnWNWjh0QB7IVqRgAiarsW4o";z.t=+new Date;$.
type="text/javascript";e.parentNode.insertBefore($,e)})(document,"script");
</script>
<!--End of Zopim Live Chat Script-->
<?php }
<?php endif; ?>
When I add this, it breaks the entire site, nothing loads anywhere. I've tried it with and without the
<?php endif; ?>
at the end, thinking that it may be duplicating with the
<?php }
I actually have two different conditional statements I need to add, each for a different page. I don't want to use the plugin that allows PHP & JavaScript in the pages themselves for security reasons, which is what I used to use.
Can anyone tell me what I'm doing wrong, and/or how to add this particular JavaScript to only page #17 (which I'm guessing will also show me how to add a PHP statement and link to single-use stylesheet that I need on a different page)?
It's hard to give a definite answer without seeing the rest of the page code but in my opinion you don't need the <?php endif; ?>.
Simply replace
<?php }
<?php endif; ?>
with
<?php } ?>
and it should work.
I'm trying to insert php variable in a javascript code but unable to get the result. Code is:
<?php $twitterusername = get_option_tree( 'twitter_name', '', 'true' );?>
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=$twitterusername&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
Actually my text editor is not marking $twitterusename inside the script as valid php code. Please help me out.
Try this:
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?php echo $twitterusername;?>settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
As it's a PHP variable, you need to echo it out with PHP, not javascript.
So you need to replace
$twitterusername
with
<?php echo $twitterusername; ?>
Even though it's inside the <script> tags, it's going to echo out whatever $twitterusername is as long as it's in PHP tags.
If your server supports shortcode, you could use
<?=$twitterusername;?>
making it slightly shorter.
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?=$twitterusername;?>settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
It is not valid php code, you need to mark it as so using <?php echo $twitterusername; ?>.
Your final code would look like:
<?php $twitterusername = get_option_tree( 'twitter_name', '', 'true' );?>
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?php echo $twitterusername; ?>&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
You need to use php tags (<? ?>) to insert variable.
<script type="text/javascript">document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=<?=$twitterusername?>&settings=0,1,2,248,279,ffffff,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>
It's because you are using your PHP variable outside PHP tags.
I am facing a problem while trying to call javascript file.
I have a menu in my html file.
HOME ME ABOUT
In my second page (ME), I run a js file :
alert("running"); // to test if my browser runs my js file
$("#some_id").blabla; // to do smoething with elements
When I switch from ME to HOME, and then getting back to ME again, my browser does not run my js file once again. That means all my jQuery's "get element by id" $(#some_id") are out-to-date.
That is because I switch in my menu with ajax call.
How to force browsers to re-run this js file after an ajax call ? Thanks
HOME code :
<?php include('header.php'); ?>
<script src="js/home.js?v=<?php echo uniqid(); ?>"></script>
<script>alert("page : home");</script>
ME code :
<?php include('header.php'); ?>
<script src="js/me.js?v=<?php echo uniqid(); ?>"></script>
<script>alert("page : me");</script>
<body> ... </body>
in ME file, javascript are loaded just once.
I have a page named Index And a page chat
for check session write the code for print echo session->user;
this code work in chat.php but include chat.php in index.php echo session->user; not work
e:g
//index.php
<?php
<iframe src="./chat.php"></iframe>
enter code here
?>
//chat.php
<?php
include_once ('./session.php');
echo $session->user;
?>
chat.php printed user but index.php not print
Where is the problem?
Please help
While this problem is in Firefox not in IE problem
For me, this works for iframe, start the session like this:
header('P3P: CP="CAO PSA OUR"');
session_start();
in chat.php and index.php remember to include
session_start()
unless it is automatically started in your php.ini
session_start()