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 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料