dongqianzong4275
dongqianzong4275
2014-02-04 20:05
浏览 57

是否可以将JavaScript函数放在php文件中?

Non-programmer here, trying to figure something out.

I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.

Currently I have this on my html header:

<script type="text/javascript">
            function loaded()
            {
            document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
            }
            onload=loaded;
        </script>

Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.

I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:

<script src="phpjavafile.php"></script>

or

    <?php include ('phpjavafile.php'; ?>

and finally put the javascript into a php file like this:

<?php
    <script type="text/javascript">
                function loaded()
                {
                document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
                }
                onload=loaded;
            </script>
?>

I have tried both of these methods and neither load properly.

I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.

Thank you ahead of time.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulang9521
    doulang9521 2014-02-04 20:27
    已采纳

    By using the echo() function

    PHP

    <?php
    echo '<script>
    some javascript
    </script';
    ?>
    

    However if you are just trying to load the php on page load without any php args then just write it out of the tags.

    If you have the JavaScript is another file then using

    PHP

    <?php
    include 'path/to/javascript/file.php';
    ?>
    

    Should work.

    You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.

    Here is the code I think you are asking for:

    PHP HTML

    <?php
        $html = file_get_contents("path/to/data.html");
    ?>
    <div>
        <?php echo $html; ?>
    </div>
    

    doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css

    点赞 评论
  • douyi4205
    douyi4205 2014-02-04 20:11

    You can just output it as text in your PHP file.

    <?php 
      // Some PHP stuff here
    ?>
    
    <script type="text/javascript">
      function loaded(){
        ....
      }
      onload=loaded;
    </script>
    
    <?php 
      // Some more PHP stuff here
    ?>
    

    Alternatively, you can just link to it in the usual way from within the same file:

    <script src="path/to/your/file.js"></script>
    
    点赞 评论

相关推荐