V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
gs038538
V2EX  ›  问与答

php 下获取 url 值通过 js 处理后,如何显示在 html 的 iframe 中呢?

  •  
  •   gs038538 · 2015-05-04 12:20:40 +08:00 · 2608 次点击
    这是一个创建于 3292 天前的主题,其中的信息可能已经有所发展或是发生改变。
    需要显示的位置已经在代码那指明,请各路高人指点一二。

    url取值方法如下: http://xxx.com/test.php?v=/xxx/xxx.mp3


    代码分割线
    ------------------------------------------------------------------------------------------


    <?php
    error_reporting(0);
    header("Content-Type: text/html; charset=utf-8");
    $v=$_GET["v"];
    ?>
    <script type="text/javascript">
    var url = '<?php echo $v ?>';
    url = url.replace(/\//g,'%252F');
    </script>


    <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path='这里需要显示js处理后的值,跟前面的部分组成完整的url'" scrolling="no" frameborder="0"></iframe>


    代码分割线
    ------------------------------------------------------------------------------------------


    期望的显示结果为
    <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path=%252Fxxx%252Fxxx.mp3" scrolling="no" frameborder="0"></iframe>
    11 条回复    2015-05-04 14:51:02 +08:00
    ituren
        1
    ituren  
       2015-05-04 12:35:22 +08:00
    $('iframe.xxxxx').prop('src',$('iframe.xxxxx').prop('src')+url);
    gs038538
        2
    gs038538  
    OP
       2015-05-04 12:42:19 +08:00
    @ituren 方便给一个相对完整的代码吗?感谢您。
    ituren
        3
    ituren  
       2015-05-04 12:45:09 +08:00   ❤️ 1
    @gs038538 那要看你iframe的这段代码是怎么生成的,你方便贴你的完整代码么?
    gs038538
        4
    gs038538  
    OP
       2015-05-04 13:02:23 +08:00
    @ituren <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path='这里需要显示js处理后的值,跟前面的部分组成完整的url'" scrolling="no" frameborder="0"></iframe>

    这段代码除了js那是变量外,其余部分是固定的,直接就写到了php里 保持html的输出即可。
    现在的做法就是直接写在了php里。帖子那就是我的完整代码啦~~不需要生产iframe。
    谢谢您!
    b821025551b
        5
    b821025551b  
       2015-05-04 13:10:44 +08:00
    $('iframe.xxxxx').prop('src',$('iframe.xxxxx').prop('src')+'这里需要显示js处理后的值,跟前面的部分组成完整的url');
    如果多个iframe用到xxxxx,那就给个id吧。
    ituren
        6
    ituren  
       2015-05-04 13:27:07 +08:00
    @gs038538 那你直接用PHP拼接输出就得了, 干嘛还要用JS.
    gs038538
        7
    gs038538  
    OP
       2015-05-04 13:29:36 +08:00
    @b821025551b 这一句需要放在哪个位置呢?

    需要的结果是

    <iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path=%252Fxxx%252Fxxx.mp3" scrolling="no" frameborder="0"></iframe>

    其中
    %252Fxxx%252Fxxx.mp3
    是被js处理过的~
    原值为/xxx/xxx.mp3

    这部分:
    http://static.xxx.com/video?outlink_code=9f7144ed&path=
    是固定的。

    您跟ituren指点的方法是一样的,但是我不知道如何去使用,见笑了。谢谢。
    gs038538
        8
    gs038538  
    OP
       2015-05-04 13:32:07 +08:00
    @ituren 是哦~~
    php需要处理 /xxx/xxx.mp3 替换字符串/为%252F %252Fxxx%252Fxxx.mp3
    然后放到 iframe的 “ http://static.xxx.com/video?outlink_code=9f7144ed&path= 替换的结果 ”...
    然后我又不知道怎么把变量传过去了。
    ituren
        9
    ituren  
       2015-05-04 13:37:54 +08:00   ❤️ 1
    @gs038538
    echo '<iframe class="xxxxx" width="480" height="360" src="http://static.xxx.com/video?outlink_code=9f7144ed&path='.$v.'" scrolling="no" frameborder="0"></iframe>';

    不用转换斜杠
    gs038538
        10
    gs038538  
    OP
       2015-05-04 13:45:58 +08:00
    @ituren 解决了!谢谢~~ '.$v.' 只需要这样就可以了!完全可以百度查到,打扰您了。谢谢。
    msg7086
        11
    msg7086  
       2015-05-04 14:51:02 +08:00
    你这个问题问得不对,主要是因为在你的网页上,PHP是比JS先执行的。也就是说,在执行到你这个iframe的时候,上头的js代码还未开始执行呢。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4186 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:12 · PVG 13:12 · LAX 22:12 · JFK 01:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.