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 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效