开发者

PHP variable isn't working for height and width

<head>
<script type="text/javascript">
<!--

 var viewportwidth;
 var viewportheight;

 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 }

// IE6 in standards compliant mode (i.e. with a valid 开发者_开发知识库doctype as the first line in the document)

 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }

 // older versions of IE

 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }

//-->
</script>
    <?php 
        $sizeh = '<script type="text/javascript">
          document.write(viewportheight);</script>';
        $sizew = '<script type="text/javascript">
          document.write(viewportwidth);
    </script>';
    ?>
</head>

//...

    <?php

<iframe  id="frame2" src="'.$row['url'].'" height="<script type=\'text/javascript\'>document.write(viewportheight);</script>px" width="'.$sizew.'px"  ></iframe> ';

?>

// The above still can't handle changing the values of height and width

Passing the above values from the script to iframe isn't working although printing the variables shows that they have values inside them

echo $sizeh; // shows ex:1093
echo $sizew; // shows ex:900

Anyone please assist !!!


There is a lot wrong here.

First, you can't mix PHP and JavaScript together like that. All PHP is processed before it's sent to the browser, and all JavaScript is processed after being sent to the browser. All you're doing is assigning those literal strings to $sizeh and $sizew. Those variables will not contain the viewport sizes, as those values can only be created by JavaScript, which happens after your PHP code runs.

Also, you can't use PHP variables outside of PHP. Once you end PHP parsing with ?>, you are no longer in PHP and the variables can no longer be accessed.

You need to echo out your PHP variables from within a <?php and ?> tags. For example:

<iframe  id="frame2" 
         src=" <?php echo $row['url']; ?>" 
         height="<?php echo $sizeh; ?> px" 
         width="<?php echo $sizew; ?> px"  
         >
</iframe>

But again, those variables don't have what you think they have in them. You either need to set those variables to have integer values from within PHP, or you need to scrap the whole thing and set the iframe dimensions using only JavaScript.

Edit: I'm not great with JavaScript, so there is almost definitely a better way of doing this (*cough* jQuery *cough*), but this should work well enough:

First, you need to create a JavaScript function inside of a <script> block in your <head> section. Name it something like resize_iframe. In this function, you will capture the <iframe> element and set its size:

<script type="text/javascript">
  function resize_iframe() {
    var iframe = document.getElementById("my_iframe");
    iframe.style.height = viewportheight + "px";
    iframe.style.width  = viewportwidth  + "px";
  }
</script>

Next, in your <body> section, you will create your HTML page as normal. The two things to be careful of here are when you're making your <iframe> element, you need to:

  • Make sure you give the element an id value that is the same as the one you used in the JavaScript function
  • When setting the src attribute on the iframe, you need to remember to wrap the PHP echo in <?php and ?> tags

Like this:

<iframe id="my_iframe"
        src="<?php echo $row['url']; ?>"
></iframe>

Finally, you want to call your resize_iframe function from a <javascript> block that occurs after the rest of the document is loaded. For example, put it right before the closing </body> tag for the document. That way, the browser will have already created the <iframe> element you're capturing, so it will exist for the JavaScript to modify. When that function runs, it will resize the <iframe>:

<script type="text/javascript"> resize_iframe(); </script>
</body>

One more thing: I'm not sure where you're getting the values for viewportwidth and viewportheight, but those aren't built-in variables for JavaScript. I can only assume you have other code that creates these values. If not, you will need to write it.


I think that as your variables have " in them , you're ending up with:

<iframe  id="frame2" src="'.row['ur;'].'"
height="<script type="text/javascript">
document.write(viewportheight);</script>px"
width="<script type="text/javascript">document.write(viewportwidth);</script>px">
</iframe>

so the "height" and "width" attributes only contains <script type= and the rest is garbage so to speak

I assume that the "<iframe...." line is enclosed within <?php...?> tags and preceded by an echo of some form. If not, you're in more trouble

PS - sorry about the formatting


php can only be used between php tags you need to type

<?php echo $sizeh; ?>

or

<?=$sizeh;?>

if your host supports short tags.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜